"Variable Not Defined"

J

JohnB

Hi.

I've been shoehorning some objects from a sample "Images in Access" mdb into
one of my mdbs, both Access 97 and mostly successfully. But when I use a
certain form as a subform in my mdb and run it, I get a Compile Error:
"Variable Not Defined" message. This does not occur when the form is used in
the origin mdb, as a main form. The problem code is "Cancel = True" and I've
just commented-out the line, with no obvious ill effects (although I believe
this will allow an unwanted error message to appear in certain
circumstances). Now I've just added a command button (cmdLinkToPhoto) to the
subform with some code which includes another "Cancel = True" line and this
errors as well. Can anyone tell me why this is happening when I use the form
as a subform in a different mdb and how to fix it? I've copied the full code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any field is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] = "" Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile] = "" Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name, Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the user to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files (*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
End Sub
 
M

MacDermott

See this line?
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
This tells us that the code was originally designed to run in a Form_Open
event, where Cancel is defined as an argument of the procedure.
Click events don't define this argument; they can't be cancelled.

HTH

JohnB said:
Hi.

I've been shoehorning some objects from a sample "Images in Access" mdb into
one of my mdbs, both Access 97 and mostly successfully. But when I use a
certain form as a subform in my mdb and run it, I get a Compile Error:
"Variable Not Defined" message. This does not occur when the form is used in
the origin mdb, as a main form. The problem code is "Cancel = True" and I've
just commented-out the line, with no obvious ill effects (although I believe
this will allow an unwanted error message to appear in certain
circumstances). Now I've just added a command button (cmdLinkToPhoto) to the
subform with some code which includes another "Cancel = True" line and this
errors as well. Can anyone tell me why this is happening when I use the form
as a subform in a different mdb and how to fix it? I've copied the full code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any field is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] = "" Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile] = "" Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name, Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the user to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files (*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
End Sub
 
J

JohnB

Thanks for the reply.

Yes you are correct, the form was originally opened by another form, but I
now have it opening from a command button and I need the code:

If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
Cancel = True
End If

to run at open so that the form opening is cancelled if there is something
in txtImagePathAndFile2.

How do I make this happen if I can't use Cancel = true?

Thanks again, JohnB

MacDermott said:
See this line?
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
This tells us that the code was originally designed to run in a Form_Open
event, where Cancel is defined as an argument of the procedure.
Click events don't define this argument; they can't be cancelled.

HTH

JohnB said:
Hi.

I've been shoehorning some objects from a sample "Images in Access" mdb into
one of my mdbs, both Access 97 and mostly successfully. But when I use a
certain form as a subform in my mdb and run it, I get a Compile Error:
"Variable Not Defined" message. This does not occur when the form is used in
the origin mdb, as a main form. The problem code is "Cancel = True" and I've
just commented-out the line, with no obvious ill effects (although I believe
this will allow an unwanted error message to appear in certain
circumstances). Now I've just added a command button (cmdLinkToPhoto) to the
subform with some code which includes another "Cancel = True" line and this
errors as well. Can anyone tell me why this is happening when I use the form
as a subform in a different mdb and how to fix it? I've copied the full code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any field is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] = "" Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile] = "" Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name, Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the user to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files (*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
End Sub
 
J

JohnB

Hi again. Please ignore my last post - I got confused. Thanks for this info
but I'm not sure how to proceed. The code that I posted, minus the
cmdLinkToPhoto code, was originally all in the On Open even of the form. I've
removed this code from the On Open event and instead, put it as the On Click
event of cmdLinkToPhoto, which I added to the form. Then I used the form as a
subform. So, when I select the relevant main form record, I can click on
cmdLinkToPhoto and the code runs. I introduced the "If Not
IsNull(Me![txtImagePathAndFile2]) Then....." code to check if a photo record
has already been created and if so, to stop the process continuing. How can I
achieve this if Cancel = True can't be used?

Thanks again. JohnB

MacDermott said:
See this line?
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
This tells us that the code was originally designed to run in a Form_Open
event, where Cancel is defined as an argument of the procedure.
Click events don't define this argument; they can't be cancelled.

HTH

JohnB said:
Hi.

I've been shoehorning some objects from a sample "Images in Access" mdb into
one of my mdbs, both Access 97 and mostly successfully. But when I use a
certain form as a subform in my mdb and run it, I get a Compile Error:
"Variable Not Defined" message. This does not occur when the form is used in
the origin mdb, as a main form. The problem code is "Cancel = True" and I've
just commented-out the line, with no obvious ill effects (although I believe
this will allow an unwanted error message to appear in certain
circumstances). Now I've just added a command button (cmdLinkToPhoto) to the
subform with some code which includes another "Cancel = True" line and this
errors as well. Can anyone tell me why this is happening when I use the form
as a subform in a different mdb and how to fix it? I've copied the full code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any field is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] = "" Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile] = "" Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name, Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the user to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files (*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
End Sub
 
M

MacDermott

If you just want processing to stop if your condition is met, use
Exit Sub


JohnB said:
Hi again. Please ignore my last post - I got confused. Thanks for this info
but I'm not sure how to proceed. The code that I posted, minus the
cmdLinkToPhoto code, was originally all in the On Open even of the form. I've
removed this code from the On Open event and instead, put it as the On Click
event of cmdLinkToPhoto, which I added to the form. Then I used the form as a
subform. So, when I select the relevant main form record, I can click on
cmdLinkToPhoto and the code runs. I introduced the "If Not
IsNull(Me![txtImagePathAndFile2]) Then....." code to check if a photo record
has already been created and if so, to stop the process continuing. How can I
achieve this if Cancel = True can't be used?

Thanks again. JohnB

MacDermott said:
See this line?
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
This tells us that the code was originally designed to run in a Form_Open
event, where Cancel is defined as an argument of the procedure.
Click events don't define this argument; they can't be cancelled.

HTH

JohnB said:
Hi.

I've been shoehorning some objects from a sample "Images in Access"
mdb
into
one of my mdbs, both Access 97 and mostly successfully. But when I use a
certain form as a subform in my mdb and run it, I get a Compile Error:
"Variable Not Defined" message. This does not occur when the form is
used
in
the origin mdb, as a main form. The problem code is "Cancel = True"
and
I've
just commented-out the line, with no obvious ill effects (although I believe
this will allow an unwanted error message to appear in certain
circumstances). Now I've just added a command button (cmdLinkToPhoto)
to
the
subform with some code which includes another "Cancel = True" line and this
errors as well. Can anyone tell me why this is happening when I use
the
form
as a subform in a different mdb and how to fix it? I've copied the
full
code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any
field
is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] = "" Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile] =
""
Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name, Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the
user
to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files (*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
End Sub
 
J

JohnB

Thank you. Do you mean I should change my code to the following? Will any
message appear as a result of this? I'm currently not able to get to the mdb
to try this, hence the question. Thanks, JohnB

If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
Exit Sub
End If


MacDermott said:
If you just want processing to stop if your condition is met, use
Exit Sub


JohnB said:
Hi again. Please ignore my last post - I got confused. Thanks for this info
but I'm not sure how to proceed. The code that I posted, minus the
cmdLinkToPhoto code, was originally all in the On Open even of the form. I've
removed this code from the On Open event and instead, put it as the On Click
event of cmdLinkToPhoto, which I added to the form. Then I used the form as a
subform. So, when I select the relevant main form record, I can click on
cmdLinkToPhoto and the code runs. I introduced the "If Not
IsNull(Me![txtImagePathAndFile2]) Then....." code to check if a photo record
has already been created and if so, to stop the process continuing. How can I
achieve this if Cancel = True can't be used?

Thanks again. JohnB

MacDermott said:
See this line?
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
This tells us that the code was originally designed to run in a Form_Open
event, where Cancel is defined as an argument of the procedure.
Click events don't define this argument; they can't be cancelled.

HTH

Hi.

I've been shoehorning some objects from a sample "Images in Access" mdb
into
one of my mdbs, both Access 97 and mostly successfully. But when I use a
certain form as a subform in my mdb and run it, I get a Compile Error:
"Variable Not Defined" message. This does not occur when the form is used
in
the origin mdb, as a main form. The problem code is "Cancel = True" and
I've
just commented-out the line, with no obvious ill effects (although I
believe
this will allow an unwanted error message to appear in certain
circumstances). Now I've just added a command button (cmdLinkToPhoto) to
the
subform with some code which includes another "Cancel = True" line and
this
errors as well. Can anyone tell me why this is happening when I use the
form
as a subform in a different mdb and how to fix it? I've copied the full
code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any field
is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] = "" Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile] = ""
Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name,
Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the user
to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files
(*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
End Sub
 
J

JohnB

No need to reply. I've tried using the code I last posted to you and it
works perfectly. Thanks for all the help. JohnB
MacDermott said:
If you just want processing to stop if your condition is met, use
Exit Sub


JohnB said:
Hi again. Please ignore my last post - I got confused. Thanks for this info
but I'm not sure how to proceed. The code that I posted, minus the
cmdLinkToPhoto code, was originally all in the On Open even of the form. I've
removed this code from the On Open event and instead, put it as the On Click
event of cmdLinkToPhoto, which I added to the form. Then I used the form as a
subform. So, when I select the relevant main form record, I can click on
cmdLinkToPhoto and the code runs. I introduced the "If Not
IsNull(Me![txtImagePathAndFile2]) Then....." code to check if a photo record
has already been created and if so, to stop the process continuing. How can I
achieve this if Cancel = True can't be used?

Thanks again. JohnB

MacDermott said:
See this line?
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
This tells us that the code was originally designed to run in a Form_Open
event, where Cancel is defined as an argument of the procedure.
Click events don't define this argument; they can't be cancelled.

HTH

Hi.

I've been shoehorning some objects from a sample "Images in Access" mdb
into
one of my mdbs, both Access 97 and mostly successfully. But when I
use
(cmdLinkToPhoto)
to
the
subform with some code which includes another "Cancel = True" line and
this
errors as well. Can anyone tell me why this is happening when I use the
form
as a subform in a different mdb and how to fix it? I've copied the full
code
for the form below and left both "Cancel = True" lines commented-out and
marked in this post with a long line of X's.

Thanks for any help, JohnB

Option Compare Database
Option Explicit

Dim ysnSaveButtonClicked As Boolean
Private Function Validate() As Boolean
' Purpose : Validate that all the information has been entered before
saving
' Parameters : None
' Returns : Boolean: True if there is information; False if any field
is
null or zero-length string
' Created/Modified : 8/9/99,
On Error GoTo PROC_ERR
Dim msg As String
'If IsNull(Me![txtImageShortName]) Or Me![txtImageShortName] =
""
Or _
'IsNull(Me![txtImageName]) Or Me![txtImageName] = "" Or _

If IsNull(Me![txtImagePathAndFile]) Or Me![txtImagePathAndFile]
=
""
Or
_
IsNull(Me![imgTheImage].Picture) Or Me![imgTheImage].Picture =
"" Then
msg = "To save, there must be information in Image Short Name,
Image
Name, and the Image itself."
msg = msg & "Please enter the information and try again or Undo."
MsgBox msg, vbOKOnly + vbInformation, "Images in Access Example"
Validate = False
Else
Validate = True
End If
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Err: " & Err.Number & " : " & Err.Description & _
" in Function Validate()", vbCritical, "Images in Access Example"
Resume PROC_EXIT
End Function

Private Sub cmdLinkToPhoto_Click()
' Purpose : Use the API to open the Windows Common Dialog for the user
to
select a file
' Parameters : Cancel -- if set to true on Exit, cancels opening the form
' Created/Modified : 6/24/99,
'On Error GoTo ErrHandler
If Not IsNull(Me![txtImagePathAndFile2]) Then
MsgBox "Photo already allocated"
'Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXX
End If


Dim lngFlags As Long
Dim strFilter As String
Dim strPathAndFile As String
Me.AllowDeletions = False
strFilter = ahtAddFilterItem(strFilter, "Compressed Image Files
(*.jpg,
*.jff, *.gif, *.tiff )", _
"*.JPG;*.JFF,*.GIF,*.TIF")
strFilter = ahtAddFilterItem(strFilter, "Uncompressed Image Files
(*.bmp, *.wmf)", "*.BMP, *.WMF")
' strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)",
"*.TXT")
strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
strPathAndFile = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Choose an Image File")
If Len(strPathAndFile) > 0 Then
MsgBox "You selected: " & strPathAndFile
Me![imgTheImage].Picture = strPathAndFile
Else
MsgBox "You didn't select a file", , "Images in Access Example"
' Cancel = True XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End If
' Since you passed in a variable for lngFlags,
' the function places the output flags value in the variable.
Exit_Sub:
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
Form_Open", vbExclamation, "Images in Access Example"
Resume Exit_Sub
End Sub


Private Sub cmdUndo_Click()
' Purpose : Undo changes to recrod
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdUndo_Click
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
End If
DoCmd.Close acForm, Me.Name
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdUndo_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdUndo_Click
End Sub
Private Sub cmdSave_Click()
' Purpose : Save the Record
' Parameters : None
' Created/Modified : 10/8/99,
On Error GoTo Err_cmdSave_Click
'If Validated, Save; if not, just return to Form
Me![txtImagePathAndFile] = Me![imgTheImage].Picture
If Validate Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Echo False
' Forms![frmImageFileSummaryList].Requery
DoCmd.Echo True
DoCmd.Close acForm, Me.Name
End If
Exit_cmdSave_Click:
On Error Resume Next
Exit Sub
Err_cmdSave_Click:
MsgBox "Error " & Err.Number & " : " & Err.Description & " in
cmdSave_Click", vbExclamation, "Images in Access Example"
Resume Exit_cmdSave_Click
End Sub

Private Sub Form_Current()
Me![imgTheImage].Picture = ""
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

Top