[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.
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:
Dim strSQL As String
Dim rs As ADODB.Recordset
strSQL = "Select From <your table name> WHERE <your field name> = '" & <your string from the CSV file> & "'"
rs.Open strSQL, <your connection>
If rs.EOF Then
'the record doesn't exist, so do a SQL insert
Else
'the record exists, so do a SQL update
End If
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!