2
Sub LogCheck()
    Dim cn As Object
    Dim rs As Object
    Dim StrSql As String
    Dim strConnection As String
    Dim AppPath As String
    Set cn = CreateObject("ADODB.Connection")
    AppPath = Application.ActiveWorkbook.Path
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=C:\ceo.accdb;"
    cn.Open strConnection
    S_ID = Sheets("My").Range("A1").Value
    StrSql = "SELECT * FROM EDO Where ID = ' " & S_ID & " '"
    rs.Open StrSql, cn
    If rs = Null Then
        MsgBox "Record Not found"
    Else
        MsgBox "Record Found"
    End If
End Sub

I am unable to run this code. Its showing error. Please help me out. Thanks! Here S_ID is the data which I would like to search from table & ID is the primary key in the EDO Table.

11
  • 1
    Can you add the error that you are getting Commented Jan 7, 2016 at 18:16
  • 1
    there probably should not be spaces between the ' and the ". like this: "SELECT * FROM EDO Where ID = '" & S_ID & "'" unless your data has a preceding and trailing space. Commented Jan 7, 2016 at 18:18
  • Run Time Error 91 Object variable or With block variable not set Commented Jan 7, 2016 at 18:36
  • What line is highlighted in yellow when this breaks? Commented Jan 7, 2016 at 18:38
  • try declaring rs as recordset. Dim rs As New ADODB.Recordset Commented Jan 7, 2016 at 18:40

2 Answers 2

0

In this case you may detect if the recordset is empty checking .EOF property:

Sub TestIfRecordFound()

    Dim strConnection As String
    Dim strID As String
    Dim strQuery As String
    Dim objConnection As Object
    Dim objRecordSet As Object

    strConnection = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source='C:\ceo.accdb';"
    strID = Sheets("My").Range("A1").Value
    strQuery = _
        "SELECT * FROM EDO WHERE ID = '" & strID & "';"

    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Open strConnection
    Set objRecordSet = objConnection.Execute(strQuery)

    If objRecordSet.EOF Then
        MsgBox "Record Not found"
    Else
        MsgBox "Record Found"
    End If

    objConnection.Close

End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

If Id is numeric than the sql should be:

StrSql = "SELECT * FROM EDO WHERE Id = " & S_ID

You also did not define S_ID, so it will be handle as a variant here. If you still get an error, you might have to make it "& CStr(S_ID)".

6 Comments

Here wdate is current date & W_Date is the field in the table with Date/Time data type. but its showing record not found as there are 10 records existing in the table. StrSql = "SELECT * FROM ceo WHERE wDate = " & CDate(W_Date) ' If objRecordSet.EOF Then MsgBox "Record Not found" Else MsgBox "Record Found" End If'
Date fields in an access DB contain date and time, so your select will not work unless all dates were stored with just the date. You probably need to modify you select as follows: "SELECT * FROM ceo WHERE " & CDate(Format(wDate,"DD/MM/YYYY")) & " = " & CDate(W_Date). You might also have to convert both sides of the equal sign to string by using CStr.
Its showing error type mismatch. StrSql = "SELECT * FROM ceo WHERE " & CDate(Format(wDate, "MM/DD/YYYY")) & " = " & CDate(W_Date)
Try: "SELECT * FROM ceo WHERE #" & CStr(CDate(Format(wDate, "MM/DD/YYYY"))) & "# = #" & CStr(CDate(W_Date))) & "#"
This one also showing same error. May I store date as string in database table.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.