|
-
Jan 9th, 2007, 09:14 AM
#1
Thread Starter
New Member
[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.
-
Jan 9th, 2007, 11:05 AM
#2
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
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
-
Jan 9th, 2007, 11:56 AM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|