I am trying to compare one column of an excel sheet with the database table.If the value in the column matches with the data in the table,the excel has to be populated with another value in the table in a new column.If match is not found the value in the new column should be kept as "X".
Dim i As Integer
i = 1
datafile = "database.accdb"
With cn
.Provider = "microsoft.ACE.OLEDB.12.0"
.ConnectionString = datafile
End With
cn.Open
Dim oXL As Object
Set oXL = CreateObject("Excel.Application")
Dim oBook As Object
Set oBook = oXL.Workbooks
Dim oSheet As Object
'Set oSheet = oXL.Worksheets(1)
Dim vValue As Variant
Set oBook = oXL.Workbooks.Open("C:\s.xlsx")
Set oSheet = oBook.Worksheets("Sheet1")
Do Until (i >= 5)
vValue = oSheet.Cells(i, 5).Value
MsgBox vValue
Set rs = cn.Execute("select * from table where ID = vValue ")
If (rs.EOF = False) Then
oSheet.Cells(i, 5).Value = rs!field
Else
oSheet.Cells(i, 5).Value = "X"
End If
rs.Close
i = i + 1
Loop
end sub
I am getting error while executing the sql query.
Dim i As Integer
i = 1
datafile = "database.accdb"
With cn
.Provider = "microsoft.ACE.OLEDB.12.0"
.ConnectionString = datafile
End With
cn.Open
Dim oXL As Object
Set oXL = CreateObject("Excel.Application")
Dim oBook As Object
Set oBook = oXL.Workbooks
Dim oSheet As Object
'Set oSheet = oXL.Worksheets(1)
Dim vValue As Variant
Set oBook = oXL.Workbooks.Open("C:\s.xlsx")
Set oSheet = oBook.Worksheets("Sheet1")
Do Until (i >= 5)
vValue = oSheet.Cells(i, 5).Value
MsgBox vValue
Set rs = cn.Execute("select * from table where ID = vValue ")
If (rs.EOF = False) Then
oSheet.Cells(i, 5).Value = rs!field
Else
oSheet.Cells(i, 5).Value = "X"
End If
rs.Close
i = i + 1
Loop
end sub
I am getting error while executing the sql query.