Results 1 to 13 of 13

Thread: Message box problem [Resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Message box problem [Resolved]

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim Search As String
    3.     Dim sql As String
    4.    
    5.      Search = InputBox("Enter Client ID", "Search Record")
    6.    
    7.     data1.Recordset.FindFirst "[Client ID]= '" & Search & "'"
    8.     sql = "SELECT * FROM Table1 WHERE [Client ID] = '" & Search & "'"
    9.         data1.RecordSource = sql
    10.         data1.Refresh
    11.        
    12.         If data1.Recordset.NoMatch Then
    13.          
    14.             MsgBox "Record Not Found!", vbExclamation, "Search Record"
    15.         Else
    16.             MsgBox "Record Found!", vbInformation, "Search Record"
    17.         End If            
    18. End Sub

    I wrote the above code to retrieve more than one records of a same client, and these records will be showed in a data grid. When record found/match, there will be a mesage box which displays "Record Found!", but the problem is when there is no record match, the message box still displays "Record Found!". I can't see anything wrong with the code/statement, any suggestion to make the message box display "Record Not Found!" when there is no record match?

    Thanks in advance!
    Last edited by hbin; Sep 20th, 2004 at 03:21 AM.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Insert this after the Refresh line:

    VB Code:
    1. Debug.Print data1.Recordset.NoMatch

    My guess is that NoMatch's value is always zero/false. If it would be something else, you'd constantly get Not Found messagebox.

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    you prolly need to compare it to -1, 0, or 1. Use an = and an else.
    it prolly returns -1 if not found or something like this that causes the false.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    VB Code:
    1. Debug.Print data1.Recordset.NoMatch

    I inseted it after the Refresh line but it still can't works. BTW, what is the meaning of Debug.Print?

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    There is a window called Immediate when your program is in runtime. When you use Debug.Print, the variable you give to it appears in the Immediate window. You can use this to find out the contents of your variables on runtime so you can debug your program and find out what might be wrong.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Question

    I only see the word "False" in the Immediate window, what does that means?

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    that it did not find a match. it would have been true if it did.

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    try changing
    Code:
    If data1.Recordset.NoMatch Then
    to
    Code:
    If data1.Recordset.NoMatch = True Then
    is the Search the same data type as ID is?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Originally posted by dglienna
    try changing
    Code:
    If data1.Recordset.NoMatch Then
    to
    Code:
    If data1.Recordset.NoMatch = True Then
    is the Search the same data type as ID is?
    I tried your code, but it is still the same.

    Yeah, the Search is the same data type as ID, which is String.

  10. #10

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    use debug.print to check each value. or else step through.

  12. #12
    Member
    Join Date
    Sep 2004
    Posts
    37
    Try the ffg. codes:

    Dim Search As String
    Dim sql As String

    Search = InputBox("Enter Client ID", "Search Record")

    sql = "SELECT * FROM Table1 WHERE [Client ID] = '" & Search & "'"
    data1.RecordSource = sql
    data1.Refresh

    if data1.recordset.bof and data1.recordset.eof then
    MsgBox "Record Not Found!", vbExclamation, "Search Record"
    else
    MsgBox "Record Found!", vbInformation, "Search Record"
    End If

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Thanks ponjaps, your code work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width