I am trying to extract data from Access using a column of excel.
I have tried the following code but its not taking too much time when # of rows in excel exceed 5k rows. Does anyone know of a better way to reference the excel data to get the results:
Sub ddd()
Const dbloc As String = "C:\Users\mysystem\Downloads\Database11.accdb"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim xlbook As Workbook
Dim xlsheet As Worksheet
Dim a As Long
Dim SQL As String
Set xlbook = ActiveWorkbook
Set xlsheet = xlbook.Worksheets(1)
xlsheet.Range("B2:Z100000").ClearContents
Set db = OpenDatabase(dbloc)
SQL = "SELECT Material, MPN "
SQL = SQL & "FROM Sheet2 "
SQL = SQL & "WHERE Material IN ("
Dim r As Range
For Each r In Range("A2:A19098")
SQL = SQL & r.Text & ","
Next r
SQL = Left(SQL, Len(SQL) - 1) 'Drop last comma
SQL = SQL & ")"
' i want to change this for loop because my range might vary from 80-100k
rows and this method is not working. i got a suggestion here that i can
use a table for this. But i am new to macros and access and not sure of
the syntax. Can anyone please help with the syntax. Assuming tablename for
the excel data is column1 and ranges from a2:a100000
Set rs = db.OpenRecordset(SQL)
', dbOpenSnapshot)
If rs.RecordCount = 0 Then
MsgBox "No data retrieved from database", vbInformation + vbOKOnly, "No
Data"
GoTo SubExit
Else
rs.MoveLast
recCount = rs.RecordCount
rs.MoveFirst
End If
xlsheet.Range("C2").CopyFromRecordset rs
End Sub
Any help will me much appreciated. Thanks!!
using a column? .... i think that you wanted to say... but it is taking too much time ...... please update your question