Results 1 to 3 of 3

Thread: [RESOLVED] Searching recordset question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    3

    Resolved [RESOLVED] Searching recordset question

    I have been asked to write a VB6 program to manipulate data in a SQL database. I am learning as I go, and am stuck. I have a form that asks for user input from a file and I need to take the results of that input and look at a data table to see if a certain record exists, ie match a specific field in a table against the input. The input comes in the form of a string from a CSV file. I need to check the existing table to see if this record already exists and if not, add it by calling a stored procedure. I have connectivity to the database and can print out the field for each record, but my attempts to verify if the input already exists have failed. I have tried a While Not EOF loop with an If statement to check "if strInput = objRec("name") Then" with the intention of using a Boolean variable if the two match, but that just does not work. I have a dummy file that has entries that are already in the db, but I don't get a match. I have also tried using the Find method, but I don't know how to incorporate since Find just stops on the field if it finds it??? This is the first ADO program I have attempted and I am struggling. I have searched the internet for sample code, but I am out of ideas. I would appreciate suggestions on the best way to approach searching a recordset to see if a given field exists and calling another procedure if it does not.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Searching recordset question

    If I understand what you want to do, this will do it. It will insert records if the table doesn't contain a record with the CSV data in the field, and update records that do exist.
    VB Code:
    1. Dim strSQL As String
    2. Dim rs As ADODB.Recordset
    3.  
    4.   strSQL = "Select From <your table name> WHERE <your field name> = '" & <your string from the CSV file> & "'"
    5.   rs.Open strSQL, <your connection>
    6.   If rs.EOF Then
    7.     'the record doesn't exist, so do a SQL insert
    8.   Else
    9.     'the record exists, so do a SQL update
    10.   End If
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    3

    Re: Searching recordset question

    Wow, that was simple. I had to edit slightly to fit my connection setup, but it is working. I have struggled with this for hours, thank you!

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