George Too
Registered User.
- Local time
- Today, 12:27
- Joined
- Aug 12, 2002
- Messages
- 198
Hello all,
I have an unbound form with an unbound subform. Users enter the record they want desplayed in the subform by entering the records id# and pressing a button. If the record is found it is displayed in the subform, if it's not found then the user is prompted to enter data for it using the same subform. All works fine except the button used to enter the data in the tables.
The button in question gets its caption from the previos process depending on whether the record was found or not. Then, depending on the caption, the button adds the record to either of 2 tables. The problem I'm having is that the button works properly only half the time.
Here is the short version of the code behind that button:
If Me.cmdAdd.Caption = "Add &Visit" Then
'verifies/adds data to the tblVisits Table
'open recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblVisits", dbOpenDynaset)
'add recipient to the table
rst.AddNew
rst("IDNo") = Me.childSubMain.Form.Controls.Item("txtIDNo")
.....
rst.Update
rst.Close
'requery all recipients today
Me.ChildSubVisits.Requery
'brings focus to the record in question in the ChildSubVisits subform
'string to search for
str = Me.childSubMain.Form.Controls.Item("txtIDNo")
'set focus & look for record
Me.ChildSubVisits.SetFocus
Me.ChildSubVisits.Form.Controls.Item("txtIDNo").SetFocus
DoCmd.FindRecord str
End If
'NEW RECORD ********************************************
If Me.cmdAdd.Caption = "Add &New Recipient" Then
'message before saving
Response = MsgBox("The data is going to be saved." & vbCrLf & _
"Do you wish to continue?", vbExclamation + vbYesNo, "Saving Data")
If Response = vbNo Then
Me.childSubMain.SetFocus
Me.childSubMain.Form.Controls.Item("txtLastName").SetFocus
'exit the procedure
Exit Sub
End If
'Else open recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblMain", dbOpenDynaset)
'add recipient to the tblMain table
rst.AddNew
rst("IDNo") = Me.childSubMain.Form.Controls.Item("txtIDNo")
...
rst("RegisteredDate") = Me.childSubMain.Controls!txtRegisteredtDate
rst.Update
rst.Close
'lock the form
Me.childSubMain.Form.AllowEdits = False
'open visits table and add new visit
Set rst = db.OpenRecordset("tblVisits", dbOpenDynaset)
'add recipient to the table
rst.AddNew
rst("IDNo") = Me.childSubMain.Form.Controls.Item("txtIDNo")
....
rst.Update
rst.Close
'requery all recipients today
Me.ChildSubVisits.Requery
'requery childSubTodays
Me.childSubNewRecipients.Requery
'recalculate the value for last id set txtLastID = to the highest id used
Me.txtLastID = DMax("[IDNo]", "tblMain")
'moves focus to the new record window and then to the new visit
'string to search for
str = Me.childSubMain.Form.Controls.Item("txtIDNo")
'brings focus to the record in question in the ChildSubVisits subform
Me.childSubNewRecipients.SetFocus
Me.childSubNewRecipients.Form.Controls.Item("txtIDNo").SetFocus
DoCmd.FindRecord str
'string to search for
str = Me.childSubMain.Form.Controls.Item("txtIDNo")
'brings focus to the record in question in the ChildSubVisits subform
Me.ChildSubVisits.SetFocus
Me.ChildSubVisits.Form.Controls.Item("txtIDNo").SetFocus
DoCmd.FindRecord str
End If
db.Close
****************
Any ideas on where the problem is? I have done several things from separating the code in 2 different buttons but still don't work.
Thanks for your help (sorry for the long post)
George
I have an unbound form with an unbound subform. Users enter the record they want desplayed in the subform by entering the records id# and pressing a button. If the record is found it is displayed in the subform, if it's not found then the user is prompted to enter data for it using the same subform. All works fine except the button used to enter the data in the tables.
The button in question gets its caption from the previos process depending on whether the record was found or not. Then, depending on the caption, the button adds the record to either of 2 tables. The problem I'm having is that the button works properly only half the time.
Here is the short version of the code behind that button:
If Me.cmdAdd.Caption = "Add &Visit" Then
'verifies/adds data to the tblVisits Table
'open recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblVisits", dbOpenDynaset)
'add recipient to the table
rst.AddNew
rst("IDNo") = Me.childSubMain.Form.Controls.Item("txtIDNo")
.....
rst.Update
rst.Close
'requery all recipients today
Me.ChildSubVisits.Requery
'brings focus to the record in question in the ChildSubVisits subform
'string to search for
str = Me.childSubMain.Form.Controls.Item("txtIDNo")
'set focus & look for record
Me.ChildSubVisits.SetFocus
Me.ChildSubVisits.Form.Controls.Item("txtIDNo").SetFocus
DoCmd.FindRecord str
End If
'NEW RECORD ********************************************
If Me.cmdAdd.Caption = "Add &New Recipient" Then
'message before saving
Response = MsgBox("The data is going to be saved." & vbCrLf & _
"Do you wish to continue?", vbExclamation + vbYesNo, "Saving Data")
If Response = vbNo Then
Me.childSubMain.SetFocus
Me.childSubMain.Form.Controls.Item("txtLastName").SetFocus
'exit the procedure
Exit Sub
End If
'Else open recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblMain", dbOpenDynaset)
'add recipient to the tblMain table
rst.AddNew
rst("IDNo") = Me.childSubMain.Form.Controls.Item("txtIDNo")
...
rst("RegisteredDate") = Me.childSubMain.Controls!txtRegisteredtDate
rst.Update
rst.Close
'lock the form
Me.childSubMain.Form.AllowEdits = False
'open visits table and add new visit
Set rst = db.OpenRecordset("tblVisits", dbOpenDynaset)
'add recipient to the table
rst.AddNew
rst("IDNo") = Me.childSubMain.Form.Controls.Item("txtIDNo")
....
rst.Update
rst.Close
'requery all recipients today
Me.ChildSubVisits.Requery
'requery childSubTodays
Me.childSubNewRecipients.Requery
'recalculate the value for last id set txtLastID = to the highest id used
Me.txtLastID = DMax("[IDNo]", "tblMain")
'moves focus to the new record window and then to the new visit
'string to search for
str = Me.childSubMain.Form.Controls.Item("txtIDNo")
'brings focus to the record in question in the ChildSubVisits subform
Me.childSubNewRecipients.SetFocus
Me.childSubNewRecipients.Form.Controls.Item("txtIDNo").SetFocus
DoCmd.FindRecord str
'string to search for
str = Me.childSubMain.Form.Controls.Item("txtIDNo")
'brings focus to the record in question in the ChildSubVisits subform
Me.ChildSubVisits.SetFocus
Me.ChildSubVisits.Form.Controls.Item("txtIDNo").SetFocus
DoCmd.FindRecord str
End If
db.Close
****************
Any ideas on where the problem is? I have done several things from separating the code in 2 different buttons but still don't work.
Thanks for your help (sorry for the long post)
George