Check my code please

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
 
How is it not working properly?

How does the caption get changed? (Running of a different procedure)

Does the Caption change when it is suppose to?
 
Thanks for the speedy response.

1. The first IF statement works but not the second. The code just doesnt execute at all.
2. The caption is changed from a separate procedure in the after update in a combo box. It is dependent on whether the record id founf or not.
3. That's the funny part, the caption does change when it's supposed to.

Thanks,
George
 
George,

Is the caption really supposed to equal "Add &New Recipient"?

Exactly as shown?

Wayne
 
Hello Wayne,
Ah, yes, the code is behind a command button which aside from doing other things it assignes the "Add &New Recipient" to the button, or "Add &Visit". Exactly as shown.

I even separated the code into 2 different hiden buttons with the appropriate code behind it. Depending on the condition only one button would be visible, but I still get the same issue.

George
 
Try using the [ code ] and [ /code ] tags (without the spaces) to post code, it keeps the indenting alive which helps readabilty (i do hope your indenting the code...)

I would suggest Structuring the code
Code:
Private Sub Command5_Click()
'code for the button
    If Me.cmdAdd.Caption = "Add &Visit" Then
        AddVisit
        Exit Sub
    End If
    If Me.cmdAdd.Caption = "Add &New Recipient" Then
        AddNewRecipient
        Exit Sub
    End If
    GetExisting
End Sub
Sub AddVisit()
' Code to add visit
End Sub
Sub AddNewRecipient()
'code to add recipient to the table
End Sub
Sub GetExisting()
'Code to show the existing record
End Sub
This makes it much more readable. Now if you have a problem in your code its much easier to track down as the logic is in the button whereas the "working" bit is in another sub....

To correct your code try using this syntax to refer to the subform...
Me.childSubMain.controls("txtIDNo")

Regards

The Mailman
 

Users who are viewing this thread

Back
Top Bottom