|
-
Apr 20th, 2007, 02:54 AM
#1
Thread Starter
Lively Member
[RESOLVED] Comparing variable with rows in database?
Hi,
I have an application in which i have to collect a value in variable and compare with a field in database which has many rows or entries. For example variable is a string called 'name'. Now this variable will be compared with a field in database which has many rows of names in it. If value of any row matches with the value of 'name' it will give a message name already exists. If not then a new row will be added.
Basically i have an idea to do it, i will get the number of rows in a variable, run a loop with counter in it. In that i will put the if else codition. But my deliemma is how do i count the number of rows and how do i introduce a loop.
Please need help on this ASAP.
Regards,
Kaushik
-
Apr 20th, 2007, 05:01 AM
#2
Lively Member
Re: Comparing variable with rows in database?
Hi,
Start looking at using "ADODB.Connection" connection object to connect to database.
Start looking at using "ADODB.Recordset" to retrieve records from database and in your case to compare with strings.
Look at the following link and it should help.
http://www.timesheetsmts.com/adotutorial.htm
Hope I have helped.
-
Apr 20th, 2007, 05:18 AM
#3
Frenzied Member
Re: Comparing variable with rows in database?
I suspect you would even require a loop. I suppose you might be aware of ADO recordsets. Simply create one. Open it. then use
adoRs.find "databaseFieldNameToCompare = '" & yourValue & "'"
if not (adoRs.EOF) then
message name already exists
else
code to add a new row
endif
Last edited by VBFnewcomer; Apr 20th, 2007 at 05:26 AM.
-
Apr 20th, 2007, 08:10 AM
#4
Thread Starter
Lively Member
Re: Comparing variable with rows in database?
Hi,
The code is working perfecly well. Thanks guys it was really of great help.
-
Apr 21st, 2007, 04:13 AM
#5
Re: Comparing variable with rows in database?
 Originally Posted by VBFnewcomer
I suspect you would even require a loop. I suppose you might be aware of ADO recordsets. Simply create one. Open it. then use
Bear in mind this is only good for school assignments and in most cases will not be efficient for real world data (rows numbering thousands)... this is because your loading an entire table just to get values in one row or worse in one column... Worst is when you end up loading almost the entire database in memory.
-
Apr 25th, 2007, 12:15 AM
#6
Frenzied Member
Re: [RESOLVED] Comparing variable with rows in database?
Bear in mind this is only good for school assignments
Is there anyother efficient way. Taking into considertaion the nature of search.
Now this variable will be compared with a field in database which has many rows of names in it
-
Apr 25th, 2007, 03:31 AM
#7
Re: [RESOLVED] Comparing variable with rows in database?
 Originally Posted by VBFnewcomer
Is there anyother efficient way. Taking into considertaion the nature of search.
Use a SELECT query with a WHERE clause and an aggregate COUNT(). Do a search, there are hundreds of samples.
-
Apr 25th, 2007, 04:57 AM
#8
Frenzied Member
Re: [RESOLVED] Comparing variable with rows in database?
Use a SELECT query with a WHERE clause and an aggregate COUNT().
I am not sure if these work with rs.find. I presume these would be taken care of by the adoRs.open <sql statement> which would be coded before reaching rs.find lines correct me if Iam wrong.
-
Apr 25th, 2007, 07:27 AM
#9
Re: [RESOLVED] Comparing variable with rows in database?
Did you try to do a search? There's no need for rs.find and there's no need to retrieve entire list of users just to verify if name already exists.
-
Apr 25th, 2007, 11:50 PM
#10
Frenzied Member
Re: [RESOLVED] Comparing variable with rows in database?
Did you try to do a search?
NO rs.find was something a friend of mine introduced me
there's no need to retrieve entire list of users just to verify if name already exists
can u point me to certain examples. I would be thankful.
-
Apr 26th, 2007, 12:07 AM
#11
Re: [RESOLVED] Comparing variable with rows in database?
Learn SQL first:
sSQL = "SELECT * FROM sampletable", returns all columns and all records in the table
sSQL = "SELECT * FROM sampletable WHERE username = 'bob'", returns all columns for records having username of 'bob'
sSQL = "SELECT COUNT(username) AS cnt FROM sampletable WHERE username = 'bob' ", returns the number of records having username of 'bob'
Code:
Dim sSQL As String
Dim rs As ADODB.Recordset 'assume ADODB.Connection named cn already setup
sSQL = "SELECT COUNT(username) AS cnt FROM sampletable WHERE username = 'bob' "
Set rs = cn.Execute(sSQL)
If rs.Fields("cnt") > 0 Then
Msgbox "already exists"
Else
sSQL = "INSERT INTO sampletable (username) VALUES ('bob') "
cn.Execute sSQL 'add new name
End if
-
Apr 26th, 2007, 02:18 PM
#12
Re: [RESOLVED] Comparing variable with rows in database?
To simplify leinad31's code
Code:
sSQL = "SELECT username FROM sampletable WHERE username = 'bob' "
Set rs = New ADODB Recordset 'so we can reclaim the memory later
rs.Open sSQL, cn
If rs.EOF Then 'if we're at EOF, there are no records where username = 'bob'
sSQL = "INSERT INTO sampletable (username) VALUES ('bob') "
cn.Execute sSQL 'add new name
Else
Msgbox "already exists"
End if
Set rs = Nothing 'reclaim the memory
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
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
|