Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2002
    Location
    Washington D.C.
    Posts
    164

    Unanswered: Checking to see if a record exists

    Hi all,
    How can I write a VB code that will use an SQL string to see if a records exists in a table or not and return a true or false value so I can build my arguments.

    Thanks

  2. #2
    Join Date
    Nov 2002
    Location
    San Francisco
    Posts
    251
    there are more ways...... for SQL server, it would be better to use STORED PROCEDURE....

    this is universal function for all your numeric IDs in all your tables...

    Function MyIDExists(InputTableName as string, InputIDName as string, InputID As Long) As Boolean
    Dim myRecSet As New ADODB.Recordset
    Dim myConnection As String

    'Use Can use CurrentProject.connection for current opened MDB or any other connection string
    myConnection = CurrentProject.Connection
    'myConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    ' "Data Source=c:\winnt\desktop\db1.mdb;" & _
    ' "User Id=admin;" & _
    ' "Password="

    myRecSet.Open " SELECT TOP 1 1 FROM " & InputTableName & " WHERE " & InputIDName & " = " & InputID, myConnection, adOpenForwardOnly, adLockReadOnly

    If myRecSet.EOF Then
    MyIDExists = False
    Else
    MyIDExists = True
    End If

    myRecSet.Close

    End Function


    jiri
    Last edited by playernovis; 12-30-02 at 16:56.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •