Results 1 to 12 of 12

Thread: [RESOLVED] Comparing variable with rows in database?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    76

    Resolved [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

  2. #2
    Lively Member
    Join Date
    Feb 2007
    Location
    Dublin, Ireland
    Posts
    120

    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.

  3. #3
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Question 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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    76

    Resolved Re: Comparing variable with rows in database?

    Hi,

    The code is working perfecly well. Thanks guys it was really of great help.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Comparing variable with rows in database?

    Quote 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.

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    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

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Comparing variable with rows in database?

    Quote 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.

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    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.

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  10. #10
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    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.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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

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

    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
  •  



Click Here to Expand Forum to Full Width