If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
|
Thread Tools | Display Modes |
#11
|
|||
|
|||
Form Doesn't Go To New Record
Tried your class out and works great!
I'm working on fully understanding it now . What is the difference between a module and a class? Appreciate all your help. Steve "rkc" wrote in message ... "Steve" wrote in message ink.net... To try and learn something new and understand what you propose, I have some questions: 1. What is the advantage to what you propose? The code for the textbox(s) behaviour is all in one place, in a class module. 2. Where specifically does your code go? The code between class and /class goes in a new class module. 3. Set and load all the textboxes you want to have the behaviour into a collection in the form's open event. How do you do this? There around 25 fields on the form, would you then cycle through the collection in the BeforeUpdate event code? 4. There are a couple of memo fields, how are they handled? Same as text fields 5. txtbox.Value = Replace(txtbox.Value, vbCrLf, " ") Why are you replacing a CrLf with a space rather than a null string? Because you can end up with two words run together if you don't. 6. What causes Private Sub Class_Terminate() to execute? The object you create from the class definition goes out of scope. Actually all objects created in the class should be destroyed when the class goes out of scope any way. Destroying them explicity is just a habit. I posted a sample .mdb at the following url. Let me know when/if you download it so I can take it off line. "http://www8.brinkster.com/rkc/pcDataSheet.mdb |
#12
|
|||
|
|||
Form Doesn't Go To New Record
"Steve" wrote in message ink.net... Tried your class out and works great! I'm working on fully understanding it now . What is the difference between a module and a class? A class defines a creatable object. When you use Access you are using objects all the time. Recordsets, Fields, Controls are all objects defined in the DAO library. Using a Class module you get to define your own objects which you can then create and use in the same way. |
#13
|
|||
|
|||
Form Doesn't Go To New Record
Bruce,
I tried your suggestion and experienced the same behaviour described under my reply to Dirk. Steve "Bruce" wrote in message om... "Steve" wrote in message link.net... Private Sub Form_BeforeUpdate(Cancel As Integer) Dim Ctrl As Control For Each Ctrl In Me.Controls If Ctrl.ControlType acLabel And Ctrl.ControlType acCommandButton Then If Not IsNull(Ctrl.Value) Then If InStr(Ctrl.Value, vbCrLf) 0 Then MsgBox Ctrl.Controls(0).Caption & " Contains A Line Return." & vbCrLf & vbCrLf _ & "The New Item Can Not Be Saved In The Database" & vbCrLf _ & "Until The Line Return Is Removed." & vbCrLf & vbCrLf _ & "Please Remove The Line Return!" Cancel = True Ctrl.SetFocus Exit Sub End If End If End If Next Ctrl ' BeforeUpdate Event Is Used For This So InvNumber = StoreItemID When The Item Is Saved Me!InvNumber = Me!StoreItemID If Me!SetThisItemAsTheDefaultNewItem = True Then Call SetDefaultValues Else Call RemoveDefaultValues End If Me!Title.SetFocus End Sub Public Sub SetDefaultValues() On Error GoTo ErrorHandler Me!Title.DefaultValue = CQuote & Me!Title & CQuote Me!FeatureFlag.DefaultValue = CQuote & Me!FeatureFlag & CQuote Me!ShortDesc.DefaultValue = CQuote & Me!ShortDesc & CQuote Me!Description.DefaultValue = CQuote & Me!Description & CQuote Me!ThumbImage.DefaultValue = CQuote & Me!ThumbImage & CQuote Me!FullImage.DefaultValue = CQuote & Me!FullImage & CQuote If Not IsNull(Me!Category) Then Me!Category.DefaultValue = CQuote & Me!Category & CQuote Else Me!Category.DefaultValue = "" End If If Not IsNull(Me!Section) Then Me!Section.DefaultValue = CQuote & Me!Section & CQuote Else Me!Section.DefaultValue = "" End If If Not IsNull(Me!Aisle) Then Me!Aisle.DefaultValue = CQuote & Me!Aisle & CQuote Else Me!Aisle.DefaultValue = "" End If Me!Alt1.DefaultValue = CQuote & Me!Alt1 & CQuote Me!Alt2.DefaultValue = CQuote & Me!Alt2 & CQuote Me!Alt3.DefaultValue = CQuote & Me!Alt3 & CQuote Me!ListValue.DefaultValue = CQuote & Me!ListValue & CQuote Me!SalePrice.DefaultValue = CQuote & Me!SalePrice & CQuote Me!SellingPrice.DefaultValue = CQuote & Me!SellingPrice & CQuote Me!DatePurchased.DefaultValue = CQuote & Me!DatePurchased & CQuote Me!ItemCost.DefaultValue = CQuote & Me!ItemCost & CQuote Me!ShipCost.DefaultValue = CQuote & Me!ShipCost & CQuote If Not IsNull(Me!InventoryLocation) Then Me!InventoryLocation.DefaultValue = CQuote & Me!InventoryLocation & CQuote Else Me!InventoryLocation.DefaultValue = "" End If Me!Inventory.DefaultValue = CQuote & Me!Inventory & CQuote Me!ReorderPoint.DefaultValue = CQuote & Me!ReorderPoint & CQuote Me!Quantity.DefaultValue = CQuote & Me!Quantity & CQuote ExitHe Me!Title.SetFocus Exit Sub ErrorHandler: MsgBox Err.Description, , "Error# " & Err.Number Resume ExitHere End Sub Public Sub RemoveDefaultValues() Me!Title.DefaultValue = "" Me!FeatureFlag.DefaultValue = "" Me!ShortDesc.DefaultValue = "" Me!Description.DefaultValue = "" Me!ThumbImage.DefaultValue = "" Me!FullImage.DefaultValue = "" Me!Category.DefaultValue = "" Me!Section.DefaultValue = "" Me!Aisle.DefaultValue = "" Me!Alt1.DefaultValue = "" Me!Alt2.DefaultValue = "" Me!Alt3.DefaultValue = "" Me!ListValue.DefaultValue = "" Me!SalePrice.DefaultValue = "" Me!SellingPrice.DefaultValue = "" Me!DatePurchased.DefaultValue = "" Me!ItemCost.DefaultValue = "" Me!ShipCost.DefaultValue = "" Me!InventoryLocation.DefaultValue = "" Me!Inventory.DefaultValue = "" Me!ReorderPoint.DefaultValue = "" Me!Quantity.DefaultValue = "" End Sub I think I would use a somewhat different approach here. Let the form save the record. In the AfterUpdate proc of the form, save whatever key value or values you need to be able to retrieve the record that you just saved. For example, if your key value is StoreItemID and that's a long integer, do something like: dim lngPrevItemID as long ' Do this in the (General) (Declarations) section of the form so it will be available to all procs in the form Form_AfterUpdate() lngPrevItemID = Me.StoreItemID End Sub Then in the form's Current event, check to see if you're on a new record. If so, use the key value you just stored to look up the previously saved record and load in all the default values. For example, do something like the following in Form_Current(): Dim rst As Recordset If Me.NewRecord Then Set rst = CurrentDB.OpenRecordset("select * from MyTable where StoreItemID = " & lngPrevItemID Me!Field1.DefaultValue = rst!Field1 ... Me!Fieldn.DefaultValue = rst!Fieldn rst.Close Set rst = Nothing Else Me!Field1.DefaultValue = "" ... Me!Fieldn.DefaultValue = "" End If -or- Dim rst As Recordset If Me.NewRecord Then Set rst = Me.RecordSetClone rst.FindFirst "StoreItemID = " & lngPrevItemID Me!Field1.DefaultValue = rst!Field1 ... Me!Fieldn.DefaultValue = rst!Fieldn rst.Close Set rst = Nothing Else Me!Field1.DefaultValue = "" ... Me!Fieldn.DefaultValue = "" End If You might be able to do this with bookmarks as well. Hope this helps. Bruce |
#14
|
|||
|
|||
Form Doesn't Go To New Record
I previously tried Bruce's suggestion and experienced the same behaviour as
described in my reply to Dirk. Before I try the Class route, is it different from what Bruce suggested? I note that the Class uses the form's BeforeUpdate event in the same way to set the default values. Steve "rkc" wrote in message ... "Steve" wrote in message ink.net... Tried your class out and works great! I'm working on fully understanding it now . What is the difference between a module and a class? A class defines a creatable object. When you use Access you are using objects all the time. Recordsets, Fields, Controls are all objects defined in the DAO library. Using a Class module you get to define your own objects which you can then create and use in the same way. |
#15
|
|||
|
|||
Form Doesn't Go To New Record
"Steve" wrote in message ink.net... I previously tried Bruce's suggestion and experienced the same behaviour as described in my reply to Dirk. Before I try the Class route, is it different from what Bruce suggested? I note that the Class uses the form's BeforeUpdate event in the same way to set the default values. The class method is just a modified way of doing the same thing you were doing in the first place. Bruce is using a query to retrieve all the fields from the previously entered record. If you use the form in the sample .mdb you downloaded to enter new records you'll see that it does not exibit the same behaviour as you described. As far as why that is... I really don't have a clue. |
#16
|
|||
|
|||
To All In This Thread
First, thanks to everyone who responded!
I found what was causing the problem but don't know why! I experienced the same behaviour when I tried Bruce's suggestion. The one difference was that my code was in the BeforeUpdate event rather than the AfterUpdate event. Bruce's suggestion put the code that set the Default Values of all the fields in the OnCurrent event. So while experimenting, I turned off the Oncurrent code so only the BeforeUpdate code would run. Then after making some entries in the form, I clicked on the new record button and was surprised that the form did not go to a new record. I still had to click twice. This meant that the problem was in the BeforeUpdate code. The first thing I tried was to comment out the line of code setting focus on the Title control at the end of the code. (Title is the first field in the tab order) After I did this, everything surprisingly worked as expected taking only one click to go to a new record. After discovering this, I went back and tried setting focus on other fields in the form and no matter which field I picked, I experienced the same problem behaviour. So the SetFocus line of code was removed. I then activated the OnCurrent code and everything continued to work as expected!! I added a line of code in the OnCurrent event to setfocus on Title and everything continued to work as expected!! So problem solved - but don't know why setting focus to a field on the form in the BeforeUpdate event made it necessary to click the new record button twice to move the form to a new record. I tried setting the cycle property to All Records and Current Record and the problem persisted at both settings. Thanks again to everyone! Steve "Steve" wrote in message ink.net... I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any field to start. If I edit that field and then click on the new record button in the navigation buttons, the form goes to a new record and each field has the default value of the previous record. If I put the focus in any field to start, edit that field go to any other field and edit it, when I click on the new record button in the navigation buttons the form does not go to a new record. I have to click the new record button a second time to go to a new record. I also tried putting the code to set the defaults in the AfterUpdate event of the form and I get the same problem of having to click on the new record button twice. Does anyone have any thoughts as to what may be causing this behaviour? Thanks! Steve |
|
Thread Tools | |
Display Modes | |
|
|