User-defined Type not defined

Sunnylei

Registered User.
Local time
Today, 04:24
Joined
Mar 5, 2011
Messages
87
Hi
I created a search button. However, I encounted a problem when I click on the search button, there is a message box shows: "Compile error: User-defined Type not defined". I have checked it, and I didn't find any problem in the following code. Can anyone let know what is wrong?

Code:
Private Sub cmSearches_Click()
On Error GoTo Err_cmSearches_Click
    Dim qdf As DAO.querydef
    Dim LSQL  As String
    Dim LSearchString As String
    If Len(txtSearchString) = 0 Or IsNull(txtSearchString) = True Then
        MsgBox "You must enter a search string."
    Else
    Set qdf = CurrentDb.QueryDefs("querySearch")
        LSearchString = txtSearchString
        'Filter results based on search string
        LSQL = "select * from StockRecord"
        LSQL = LSQL & " where Descriptions LIKE '*" & LSearchString & "*'"
      Debug.Print LSQL
      
      
      qdf.sql = LSQL
            If DCount("*", "querysearch", "Descriptions Like '*" & LSearchString & "*'") > 0 Then
                   DoCmd.OpenQuery "querySearch"
            Else
                MsgBox "There are no records with Search string " & vbCrLf & vbTab & "--->" & LSearchString & "<---"
            End If
            
        'Clear search string
        'txtSearchString = ""
        'MsgBox "Results have been filtered.  All Descriptions containing " & LSearchString & "."
         End If
       
       End Sub
 
    Screen.PreviousControl.SetFocus
    DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70
Exit_cmSearches_Click:
    Exit Sub
Err_cmSearches_Click:
    MsgBox Err.Description
    Resume Exit_cmSearches_Click
    
End Sub

Thanks
 
Comment out this line

On Error GoTo Err_cmSearches_Click

... re-run your code and tell us where it errors.
 
Comment out this line

On Error GoTo Err_cmSearches_Click

... re-run your code and tell us where it errors.

I deleted On Error GoTo Err_cmSearches_Click, however, when I run, there is a highlight on:

Code:
Dim qdf As DAO.querydef

a message shows: Compile error: user-defined type not defined.
 
So next time you have error handling code, remove the On Error... line and re-run your code and you will see where it errors. By the way, do you know how to write comments in VBA?

Did you write the entire code?
 
So next time you have error handling code, remove the On Error... line and re-run your code and you will see where it errors. By the way, do you know how to write comments in VBA?

Did you write the entire code?

I'm not sure how to write comments in VBA, but I really want to learn it.

Thanks
 
In that case I can infer that you copied the code from somewhere and made amendments to it.

If you want to comment a line of code just prefix it with a single quote, so that line I told you about earlier will look like this:
Code:
[COLOR=Red]' [/COLOR]On Error GoTo Err_cmSearches_Click
The single quote is in red. Or you can use the Comment Block button shown in the attached image. If you can't see that button go to View > ToolBars > tick Edit. The good thing about using the button is you can comment multiple lines of code in one go. The button next to it allows you to uncomment code.

Now back to your OP, you need to add the DAO reference (i.e. Tools > References > scroll down and look for Microsoft DAO x.x Object Library > tick the box > click OK)... where x.x is the version number.
 

Attachments

  • CommentVBA.png
    CommentVBA.png
    18.9 KB · Views: 117
In that case I can infer that you copied the code from somewhere and made amendments to it.

If you want to comment a line of code just prefix it with a single quote, so that line I told you about earlier will look like this:
Code:
[COLOR=red]' [/COLOR]On Error GoTo Err_cmSearches_Click
The single quote is in red. Or you can use the Comment Block button shown in the attached image. If you can't see that button go to View > ToolBars > tick Edit. The good thing about using the button is you can comment multiple lines of code in one go. The button next to it allows you to uncomment code.

Now back to your OP, you need to add the DAO reference (i.e. Tools > References > scroll down and look for Microsoft DAO x.x Object Library > tick the box > click OK)... where x.x is the version number.

It's working now!!! Thanks
 

Users who are viewing this thread

Back
Top Bottom