Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / July 2005

Tip: Looking for answers? Try searching our database.

"Variable Not Defined"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JohnB - 27 Jul 2005 23:08 GMT
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
MacDermott - 28 Jul 2005 01:27 GMT
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.
>
[quoted text clipped - 135 lines]
> Me![imgTheImage].Picture = ""
> End Sub
JohnB - 28 Jul 2005 09:46 GMT
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

> See this line?
> ' Parameters : Cancel -- if set to true on Exit, cancels opening the form
[quoted text clipped - 156 lines]
> > Me![imgTheImage].Picture = ""
> > End Sub
JohnB - 28 Jul 2005 10:58 GMT
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

> See this line?
> ' Parameters : Cancel -- if set to true on Exit, cancels opening the form
[quoted text clipped - 156 lines]
> > Me![imgTheImage].Picture = ""
> > End Sub
MacDermott - 28 Jul 2005 23:23 GMT
If  you just want processing to stop if your condition is met, use
   Exit Sub

> 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
[quoted text clipped - 169 lines]
> > > Me![imgTheImage].Picture = ""
> > > End Sub
JohnB - 29 Jul 2005 12:18 GMT
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

> If  you just want processing to stop if your condition is met, use
>     Exit Sub
[quoted text clipped - 199 lines]
> > > > Me![imgTheImage].Picture = ""
> > > > End Sub
JohnB - 29 Jul 2005 18:05 GMT
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
> If  you just want processing to stop if your condition is met, use
>     Exit Sub
[quoted text clipped - 199 lines]
> > > > Me![imgTheImage].Picture = ""
> > > > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.