+ Reply to Thread
Results 1 to 10 of 10

Thread: SQL Search in VBA

  1. #1
    Barn Newbie Colmag is an unknown quantity at this point Colmag's Avatar
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    3

    SQL Search in VBA

    Greetings:

    I'm trying to conduct a search for a variable using a form in vbscript. I am trying to use this code but it dosen't seem to work out

    Code:
    Dim SQLstr As String
    
    Item.Value = txtlvar.Value
    Label.Value = txtlvarl.Value
    
    
    SQLstr = SELECT tblLT.[ItemNumber], tblLabelTable.[LabelNumber] FROM tblLT 
      WHERE (((tblLT.[ItemNumber])=[txtvar]) AND ((tblLT.[LabelNumber])=[txtvarl]));
    
    CurrentDb.Execute (SQLstr)
    
    If Item.Value = [itemnumber] Then
    MsgBox "Item found"
    End If
    
    If Label.Value = [labelnumber] Then
    MsgBox "Item Found"
    End If
    
    Item.value and Label.Value are textbox in the form that the user can enter for a search. I get an error when trying to run this and I really don't know much about vba and mysql commands so I'm kinda lost there.

    Any help is much appreciated.
    Last edited by don94403; July 24th, 2009 at 11:00 PM. Reason: code formatting

  2. #2
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    You have quite a few errors in your VBA code (it's not VBScript, that's a different variation of the language).

    First, when you define a string variable, the literal must be enclosed in quotation marks.

    Then you are using the wrong command. The Execute method is not appropriate for searching a table for a value. You could use OpenRecordset, but you have to define the database and recordset objects first.

    I'm confused by your saying that you have a text box named "Label". I do hope that's not the case, that would be extremely misleading, since a label is a control that the user cannot use, it's for displaying a label.

    So let's assume that you just have a text box named "Item". Your code should be:
    Code:
    Dim SQLstr As String
    Dim db As DAO.Database   ' This could also be done with ADO, different syntax
    Dim rs As DAO.Recordset
    Set db = CurrentDB
    
    SQLstr = "SELECT [ItemNumber] FROM tblLT WHERE ItemNumber = Me!Item"
    
    Set rs = db.OpenRecordset(SQLstr)
    
    rs.MoveLast
    
    If rs.RecordCount > 0 Then
       MsgBox "Item found"
    End If
    
    End If
    
    At least, that's the way I would probably code it. You really need to study the fundamentals of VBA programming.
    question = 2B || !2B

  3. #3
    Barn Newbie Colmag is an unknown quantity at this point Colmag's Avatar
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    3

    Thanks I'll try that.

    Yes I agree I don't know much about it. I just started taking this class and it's a project that's due, and they haven't gone over coding very much yet, so I'm kinda clueless. If you know of any good places to learn more about vb I'd be happy to try and learn more for myself,

  4. #4
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    A good place to start would be Google. Use search terms MS access basic tutorial.
    question = 2B || !2B

  5. #5
    Barn Regular boblarson will become famous soon enough boblarson's Avatar
    Join Date
    Jul 2008
    Location
    Portland, Oregon
    Posts
    66
    Blog Entries
    1
    Real Name
    Bob
    Rep Power
    4

    Quote Originally Posted by Colmag View Post
    Thanks I'll try that.

    Yes I agree I don't know much about it. I just started taking this class and it's a project that's due, and they haven't gone over coding very much yet, so I'm kinda clueless. If you know of any good places to learn more about vb I'd be happy to try and learn more for myself,
    Another good VBA tutorial is:

    2007 VBA

    2003 VBA
    Bob Larson
    Access MVP (2008-2009, 2009-2010, 2011-2012)

    Free samples and tutorials: http://www.btabdevelopment.com



  6. #6
    Barn Newbie Colmag is an unknown quantity at this point Colmag's Avatar
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    3

    Hi,

    Tried the code, but it keeps giving me an error message

    Run-time Error '3061'

    Too few parameters. Expected 1.

    I tried switching things around, chaning some names, nothing works I just get this error. Thanks

  7. #7
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Quote Originally Posted by Colmag View Post
    Hi,

    Tried the code, but it keeps giving me an error message

    Run-time Error '3061'

    Too few parameters. Expected 1.

    I tried switching things around, chaning some names, nothing works I just get this error. Thanks
    Have you debugged? What does this return:

    Code:
    SQLstr = "SELECT [ItemNumber] FROM tblLT WHERE ItemNumber = Me!Item"
    MsgBox SQLstr
    
    More than likely, you need to replace Me!Item with your value.
    "You'll never be as perfect as BLaaaaaaaaarche."

  8. #8
    Barn Newbie Colmag is an unknown quantity at this point Colmag's Avatar
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    3

    It brings up the Set rs = db.OpenRecordset(SQLstr) statement when I debug.

    The problem is me!item is what the user inputs for the search, so if it make it a set value then I won't be able to do the search I think.

  9. #9
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    I owe you an apology, I was too fast on the trigger. The "Me!Item" reference cannot be inside the quotation marks, because it is not a literal value, what you want is the value represented by that expression. So make this change:
    Quote Originally Posted by don94403 View Post
    Code:
    Dim SQLstr As String
    Dim db As DAO.Database   ' This could also be done with ADO, different syntax
    Dim rs As DAO.Recordset
    Set db = CurrentDB
    
    SQLstr = "SELECT [ItemNumber] FROM tblLT WHERE ItemNumber = " & Me!Item
    
    Set rs = db.OpenRecordset(SQLstr)
    
    rs.MoveLast
    
    If rs.RecordCount > 0 Then
       MsgBox "Item found"
    End If
    
    End If
    
    That should work if "ItemNumber" is really a number. If it is a text field, that line must read:
    SQLstr = "SELECT [ItemNumber] FROM tblLT WHERE ItemNumber = '" & Me!Item & "'"
    where what looks like 3 quotes is actually a single quote followed by a double quote, and what looks like 5 quotes is actually a single quote surrounded by double quotes.
    question = 2B || !2B

  10. #10
    Barn Newbie Colmag is an unknown quantity at this point Colmag's Avatar
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    3

    Quote Originally Posted by don94403 View Post
    I owe you an apology, I was too fast on the trigger. The "Me!Item" reference cannot be inside the quotation marks, because it is not a literal value, what you want is the value represented by that expression. So make this change:
    That should work if "ItemNumber" is really a number. If it is a text field, that line must read:where what looks like 3 quotes is actually a single quote followed by a double quote, and what looks like 5 quotes is actually a single quote surrounded by double quotes.
    No need to apologize, you helped me a lot. The code works now. Hopefully now that I have a little more knowledge I'll be able to get a lot closer to the proper code before I need to ask or not even ask at all.

    Thanks again.

+ Reply to Thread

Similar Threads

  1. Alphabet search??
    By Centurion in forum ASP Development
    Replies: 5
    Last Post: February 22nd, 2009, 10:59 PM
  2. when using multiple search values
    By Rebelle in forum Microsoft Access
    Replies: 2
    Last Post: February 6th, 2009, 04:03 PM
  3. search,add and edit in asp.net
    By guddu in forum .NET Development
    Replies: 34
    Last Post: January 15th, 2009, 02:53 PM
  4. Search Engine friendly URL's using classic .ASP
    By decbrad in forum ASP Development
    Replies: 4
    Last Post: December 22nd, 2008, 08:51 PM
  5. picture search
    By Centurion in forum ASP Development
    Replies: 3
    Last Post: December 2nd, 2008, 09:06 AM

Bookmarks

Posting Permissions

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

SEO by vBSEO