Compile Error Help

M

MBoozer

I took the advice of the experts in the group and made sure all my forms are
set to "Option Explicit" before distributing the db. Even though I did it
after the fact, I only have one error on compile and can't figure out what it
is: The editor highlights the RetValue on the sedcond line saying variable
not defined. I have no idea what this means except that it is not good. Any
help? Thanks.

Private Sub EditRecord_Click() 'Make the Form Editable'
RetValue = MsgBox("Warning:" & vbCrLf & "Editing a Chemical Name will change
the name of all existing records for that name. Do you wish to continue?",
vbExclamation + vbYesNoCancel, "Edit Chemical Name")

If RetValue = 6 Then
Me.AllowEdits = True 'Allow Edits To Data'
Me.ChemName.SetFocus 'Place cursor into FieldName'
ElseIf RetValue = 2 Then 'Cancel Event for Cancel Button'
DoCmd.CancelEvent
ElseIf RetValue = 7 Then 'Cancel Event For No Button'
DoCmd.CancelEvent
End If
End Sub
 
D

Douglas J. Steele

You haven't defined RetVal using a Dim statement in the routine.

Since MsgBox returns a Long Integer, you need

Dim RetVal As Long

at the beginning of the routine.
 
M

MBoozer

Thanks doug but it still comes back with the same error. I put it in the
beginning of the routine as follows:

'Make the Form Editable'
Dim RetVal As Long
RetValue = MsgBox("Warning:" & vbCrLf & "Editing an MSDS Record will
change the record permanently in the database. Do you wish to continue?",
vbExclamation + vbYesNoCancel, "Edit MSDS Record")
If RetValue = 6 Then
Me.AllowEdits = True 'Allow Edits To Data'
Me.DateAdded.SetFocus 'Place cursor into FieldName'
ElseIf RetValue = 2 Then 'Cancel Event for Cancel Button'
DoCmd.CancelEvent
ElseIf RetValue = 7 Then 'Cancel Event For No Button'
DoCmd.CancelEvent
End If
 
T

tina

hi Doug. A2003 Help says that the MsgBox() function returns an Integer
value. is this an(other) error in the Help documentation? tia, tina :)
 
D

Douglas J. Steele

Sorry, my fault. You're using RetValue as the variable, whereas I only type
RetVal. Change your declaration to agree with your variable.
 
D

Douglas J. Steele

Integer certainly makes sense, given the range of values possible (heck,
Byte would be sufficient). I'm sure declaring the variable as Integer would
work fine.

The general rule is if you make the variable to which you're assigning the
value larger than necessary, you won't have any problems, but the reverse
isn't true.

To be honest, I seldom declare a variable just to use for the purpose of
obtaining the result of a MsgBox. I'd be more inclined to use

Private Sub EditRecord_Click() 'Make the Form Editable'

Select Case MsgBox("Warning:" & vbCrLf & "Editing a " & _
"Chemical Name will change the name of all existing " & _
"records for that name. " & vbCrLf & "Do you wish to " & _
"continue?", vbExclamation + vbYesNoCancel, _
"Edit Chemical Name")

Case vbCancel
DoCmd.CancelEvent
Case vbYes
Me.AllowEdits = True
Me.ChemName.SetFocus
Case vbNo
DoCmd.CancelEvent
Case Else
' Should never happen
End Select

End Sub
 
M

MBoozer

Thanks again Doug and Tina. It works great.

Douglas J. Steele said:
Integer certainly makes sense, given the range of values possible (heck,
Byte would be sufficient). I'm sure declaring the variable as Integer would
work fine.

The general rule is if you make the variable to which you're assigning the
value larger than necessary, you won't have any problems, but the reverse
isn't true.

To be honest, I seldom declare a variable just to use for the purpose of
obtaining the result of a MsgBox. I'd be more inclined to use

Private Sub EditRecord_Click() 'Make the Form Editable'

Select Case MsgBox("Warning:" & vbCrLf & "Editing a " & _
"Chemical Name will change the name of all existing " & _
"records for that name. " & vbCrLf & "Do you wish to " & _
"continue?", vbExclamation + vbYesNoCancel, _
"Edit Chemical Name")

Case vbCancel
DoCmd.CancelEvent
Case vbYes
Me.AllowEdits = True
Me.ChemName.SetFocus
Case vbNo
DoCmd.CancelEvent
Case Else
' Should never happen
End Select

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Option Explicit 2
Changing a private function to public 4
Error 2001 0
Error 2001 0
Error 2001 0
Error 2001 message 1
Error 2001 1
Can't cancel an automatic Access message 6

Top