Results 1 to 6 of 6

Thread: Slect Statement with a Where Clause

  1. #1

    Thread Starter
    Lively Member The_Game's Avatar
    Join Date
    Apr 2006
    Posts
    115

    Slect Statement with a Where Clause

    I'm trying to create a function that will search an Access Database to see if a record already exists or not.

    I am having problems with my select statement with regards to the where part of it.


    Here's what i have so far:

    VB Code:
    1. Option Explicit
    2. Private prvCNNBatchHeader As New ADODB.Connection
    3.  
    4.  
    5. Private Function FindExistingRecord(strTransactionType As String, strBatchNumber As String) As Boolean
    6.  
    7. Dim rsBatchHeader As New ADODB.Recordset
    8.  
    9.  
    10. rsBatchHeader.Open "Select * From BatchHeader Where BatchHeader.TransactionType = strTransactionType and BatchHeader.BatchNumber = strBatchNumber", prvCNNBatchHeader, adOpenDynamic, adLockOptimistic
    11.  
    12. end Function

    I am getting an error stating: [Microsoft][ODBC Microsoft Access Driver] Too Few Parameters. Expected 2.

    Where i run the select part without the where clause, it seems to work fine, but not with the Where.

    Hope someone can help
    thanks

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Slect Statement with a Where Clause

    You have to wrap your variable in with quotation marks. Try this:

    VB Code:
    1. rsBatchHeader.Open "Select *
    2. From BatchHeader
    3. Where BatchHeader.TransactionType = '" & strTransactionType & _
    4. "' and BatchHeader.BatchNumber = '" & strBatchNumber & "'", _
    5. prvCNNBatchHeader, adOpenDynamic, adLockOptimistic
    Last edited by Mark Gambo; Apr 28th, 2006 at 01:07 PM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Slect Statement with a Where Clause

    Try
    Code:
    "SELECT * FROM BatchHeader 
    WHERE BatchHeader.TransactionType = '" & strTransactionType & "' "
    AND BatchHeader.BatchNumber = '" & strBatchNumber & "' ", prvCNNBatchHeader, adOpenDynamic, adLockOptimistic

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Slect Statement with a Where Clause

    Create a varable called strSQL

    VB Code:
    1. strSQL = "Select * From BatchHeader Where BatchHeader.TransactionType =  '" & strTransactionType & " and BatchHeader.BatchNumber = '" & strBatchNumber & "'"

    This assumes that the field types in the database are strings

    Then:
    VB Code:
    1. rsBatchHeader.Open strSQL, prvCNNBatchHeader, adOpenDynamic, adLockOptimistic

    The varables you are passing need to attached to the statement as varables and not included as part of the statement string.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Lively Member The_Game's Avatar
    Join Date
    Apr 2006
    Posts
    115

    Re: Slect Statement with a Where Clause

    OK, thanks guys i have the select statement working now, but i'm still having problems with my overall search.

    I'm trying to take two user inputs (strTransactionType, strBatchNumber) and search through an Access Database table (BatchHeader) to see if they exist.

    If they exist, i want to load the information to the form for editing and if they don't exist i want to create a new record.

  6. #6
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: Slect Statement with a Where Clause

    make sure both the fields exist in your table ,one of them is a primary key and then just use the and keyword in the where clause !!
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

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