VB macro to identify different combinations of cells - vba

I have 4 columns of data.
A is process step
C is Risk number
D is risk rating (high, moderate or low)
F is a risk moderator (control, monitor, test or blank).
I need to populate a presentation with the different combinations.
How many risks have controls and how many do not?
of the ones with controls, how many have monitors or tests, how many do not?
of the ones without controls how many have monitors or tests and how many do not?
within those different categories how many are high, moderate and low?
for the final box those without controls, monitors or tests, what process steps do the risks belong to?
Each risk effectively has a block occupied by column F containing mitigations. There can be several boxes with different items or they can be blank if there are no mitigators.
Below is code that identifies with or without controls but I am stuck on the next sections (counting the different risk ratings and breaking it further into with/without monitoring or testing.
Sub CountWithWithoutControlsv6()
Const RiskColumn As String = "C"
Const ControlColumn As String = "F"
Const StartAtRow As Long = 7
Dim r As Long
Dim ControlFound As Boolean
Dim control As Long
Dim WoControl As Long
Dim lastRow As Long
With ActiveSheet
lastRow = .Range(RiskColumn & .Rows.Count).End(xlUp).row
r = .Range(ControlColumn & .Rows.Count).End(xlUp).row
If lastRow < r Then
lastRow = r
End If
ControlFound = False
For r = StartAtRow To lastRow
If UCase(Trim(CStr(.Cells(r, ControlColumn).Value))) = "CONTROL" Then
ControlFound = True
End If
If Not IsEmpty(.Cells(r + 1, RiskColumn)) Then
'Store info for previous block each time we encounter a new "risk"
If ControlFound Then
control = control + 1
Else
WoControl = WoControl + 1
End If
ControlFound = False
End If
Next
'Store info for final "block"
If ControlFound Then
control = control + 1
Else
WoControl = WoControl + 1
End If
End With
MsgBox control & " = number of risks with controls, " & WoControl & " = number of risks without controls"
End Sub

I'd recommend you "flatten" your data so that column A contains the process step, C is Risk number, D is risk rating (high, moderate or low), F indicates whether a "control" risk moderator exists, G indicates whether a "monitor" risk moderator exists, and H indicates whether a "test" risk moderator exists.
So instead of having data such as
A C D F
Step1 Risk7 High control
monitor
test
Step2 Risk8 Low (blank)
you would instead have
A C D F G H
Step1 Risk7 High Yes Yes Yes
Step2 Risk8 Low No No No
Once in that format, it would be simple to use Excel formulae such as =COUNTIFS(F:F,"Yes",G:G,"Yes",H:H,"No").

If you followed your current approach to this problem you would ultimately achieve your goals - and the code itself would be straightforward to write. However, it would take an awfully long time to write to cover all of your cases and would be mind-numbingly tedious to write. (That you've only produced a few lines of what is, in all honesty, quite a trivial task is probably the reason for your downvote.)
However, before you launch into the remainder of the code, perhaps you would consider using ADO. It's ideally suited to this task and could complete your project in just a few lines. I've no desire to solve your problem for you, but I would like to offer some code to get you started together with a couple of query samples. I've used early binding which means I've referenced the Microsoft ActiveX Data Objects 6.1 Library (Tools ~> References...), but you could late bind if you wish.
Have a look at the code below and see if you could use it:
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim connStr As String
Dim sqlStr As String
Dim tableName As String
Dim procField As String
Dim numField As String
Dim levelField As String
Dim controlField As String
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source='C:\Users\User\Documents\StackOverflow\ADOexample.xlsm';" & _
"Extended Properties=""Excel 12.0;HDR=YES;"";"
'Define the table and field names
With Sheet1 'change to your sheet
tableName = .Name
procField = CStr(.Cells(1, "A").Value)
numField = CStr(.Cells(1, "C").Value)
levelField = CStr(.Cells(1, "D").Value)
controlField = CStr(.Cells(1, "F").Value)
End With
'Open the connection
Set conn = New ADODB.Connection
conn.Open connStr
'Some sample queries
'Sample 1
Set rs = New Recordset
sqlStr = "SELECT * FROM [" & tableName & "$]" & _
" WHERE [" & controlField & "] IS NOT NULL;"
rs.Open sqlStr, conn
Do While Not rs.EOF
Debug.Print rs.Fields(numField)
rs.MoveNext
Loop
'Sample 2
Set rs = New Recordset
sqlStr = "SELECT * FROM [" & tableName & "$]" & _
" WHERE [" & levelField & "] = 'H'" & _
" AND ([" & controlField & "] = 'MONITOR'" & _
" OR [" & controlField & "] = 'TEST');"
rs.Open sqlStr, conn
Do While Not rs.EOF
Debug.Print rs.Fields(numField)
rs.MoveNext
Loop
'Clean up objects
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing

Related

Splitting Access database via VBA takes suddenly ages

I have an MS Access database. The database version is 2002-2003 (therefore mdb). From that database I am exporting several fields into a temporary table in order to represent them on a website. As there is a field which has several comma-separated entries, I am splitting them so that each record has only one entry per field.
Imagine a German-English dictionary with the following record:
en | de
building | Gebäude,Bauwerk
I want to split it as follows:
en | de
building | Gebäude
building | Bauwerk
The VBA function that I am using used to work fine. The database has around 100.000 records. Splitting used to take around 30 minutes. Now it takes the whole day.
This is the function:
Public Sub commasplitfield4()
Dim rstObj As DAO.Recordset, dbObj As DAO.Database
Dim InsertSQL As String
Set dbObj = CurrentDb()
Set rstObj = dbObj.OpenRecordset("qry-export")
DoCmd.SetWarnings False
Do While Not rstObj.EOF
Dim memArr() As String
memArr = Split(rstObj.Fields("field4"), ",")
For i = 0 To UBound(memArr)
InsertSQL = "INSERT INTO exporttemp(field1,field2,field3,field4) VALUES (""" & rstObj.Fields("field1") _
& """, """ & rstObj.Fields("field2") _
& """, """ & rstObj.Fields("field3") & """, """ & memArr(i) & """)"
DoCmd.RunSQL (InsertSQL)
Next
rstObj.MoveNext
Loop
DoCmd.SetWarnings True
End Sub
I cannot say when exactly it started to take so long, but I can say that changing from Windows 7 to Windows 10 didn't make a difference. I am on Windows 10 for a long time and it still used to work well. Also moving from Access 2007 to 2010 and then to 2019 didn't make a difference, at least not at once.
In order to check where the error could lie I went through the following checklist:
I compact the database before starting the function
I tried to start Access in Windows 7 compatibility mode
I removed unused fields
I started the performance analyser and made the changes that were proposed (in two fields I changed the data type)
I split the database into a backend only with the tables and a frontend which contains queries and modules
I exported the content of the backend into a text file and re-imported it into a newly created backend
I stopped the Antivirus while performing the function (although Antivirus used very little processor capacity)
None of that made a notable difference.
Any idea?
The by far best answer was the one from HansUp. Instead of a whole day it takes a couple of minutes now. I cannot even thank HansUp properly because he put the solution in a side comment.
Surprisingly, there is actually little that I had to change in the code. So, the solution was to modify the code as follows:
Public Sub commasplitfield4()
Dim rstObj As DAO.Recordset, dbObj As DAO.Database
Dim InsertSQL As String
Set dbObj = CurrentDb()
Set rstObj = dbObj.OpenRecordset("qry-export")
DoCmd.SetWarnings False
Do While Not rstObj.EOF
Dim memArr() As String
memArr = Split(rstObj.Fields("field4"), ",")
For i = 0 To UBound(memArr)
InsertSQL = "INSERT INTO exporttemp(field1,field2,field3,field4) VALUES (""" & rstObj.Fields("field1") _
& """, """ & rstObj.Fields("field2") _
& """, """ & rstObj.Fields("field3") & """, """ & memArr(i) & """)"
'DoCmd.RunSQL (InsertSQL)
dbObj.Execute (InsertSQL), dbFailOnError 'this line made the difference
Next
rstObj.MoveNext
Loop
'DoCmd.SetWarnings True
End Sub
I can't explain the exact cause of your problem, but I think it takes a lot of time to loop through the recordset and loop through the Array.
The task of separating characters with commas seems to be faster using vba in Excel.
The example source data was for 1000000 records,
The contents separated by each comma were written in two per record, and the records of the converted data were tested with data of 2000000.
Import the original data of Access into Excel (Sheets(1)). (Example table2) ~~> 0.7617188 seconds
Convert the data by separating the data of the imported Sheets(1) with commas.
--> Sheets(2) ~~> 21.58594 seconds
Load data from Sheets(2) by Access applicantion.
~~> 5 minutes
Import the original data of Access
Sub exeSQLgetdata()
Dim Rs As ADODB.Recordset
Dim strConn As String
Dim i As Integer
Dim Fn As String
Dim Ws As Worksheet
Dim st, et
st = Timer
Set Ws = Sheets(1)
Fn = ThisWorkbook.Path & "\" & "Database9.accdb" '<~~ your database path & name
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Fn & ";"
Set Rs = CreateObject("ADODB.Recordset")
strSQL = "Select field1,field2, field3, field4 from [table2]" '<~~ your raw data table
Rs.Open strSQL, strConn
If Not Rs.EOF Then
With Ws
For i = 0 To Rs.Fields.Count - 1
.Cells(1, i + 1) = Rs.Fields(i).Name
Next i
.UsedRange.Offset(1).Clear
.Range("a2").CopyFromRecordset Rs
End With
End If
Rs.Close
Set Rs = Nothing
et = Timer
Debug.Print "Get Data time : " & et - st & " seconds" '<~~ get data from access database
End Sub
Convert the data by separating the data of the imported Sheets(1) with commas.
Sub splitData()
Dim vR(1 To 1000000, 1 To 4)
Dim vDB As Variant
Dim i As Long, n As Long
Dim k As Variant, v As Variant
Dim Ws As Worksheet
Dim toWs As Worksheet
Dim st, et
st = Timer
Set Ws = Sheets(1)
Set toWs = Sheets(2)
vDB = Ws.Range("a1").CurrentRegion
For i = 1 To UBound(vDB, 1)
k = Split(vDB(i, 4), ",")
For Each v In k
n = n + 1
vR(n, 1) = vDB(i, 1)
vR(n, 2) = vDB(i, 2)
vR(n, 3) = vDB(i, 3)
vR(n, 4) = v
Next v
DoEvents
Next i
With toWs
.UsedRange.Clear
.Range("a1").Resize(UBound(vR, 1), UBound(vR, 2)) = vR
End With
et = Timer
Debug.Print "Split time : " & et - st & " seconds"
End Sub

VBA - Executing ADODB.CommandText

I promised myself I would not post this because I have this delusional thought that I am too good of a programmer, yet here we are.
I have altered what I posted earlier last week trying to figure out how to write a VBA function that would write data from an Excel Range to an MS SQL Table. That worked.
Towards the end of the program, I do not know how to construct the final execution of the code; I have tried everything from using the Command.Text in the upper levels, setting it to a Recordset, then executing the recordset, but nothing will make the little VBA troll happy. Here is what I currently have written:
Sub Connection()
Dim Tbl As String
Dim InsertQuery As New ADODB.Command
InsertQuery.CommandType = adCmdText
Dim xlRow As Long, xlCol As Integer
Dim DBconnection As New ADODB.Connection
Dim ConnString As String
Dim rst As New ADODB.Recordset
Dim a As Integer, sFieldName As String
Dim db As DAO.Database
Dim CurrentDb As Database
Dim ConnectionStr
ConnectionStr = "Provider=sqloledb;Server="";Inital Catalog="";Integrated Security=SSPI;User ID="";Password="""
DBconnection.Open ConnectionStr
xlRow = 1 'only one row being used *as of now*, and that is the top row in the excel sheet
xlCol = 119 'First column of misc. data
While Cells(xlRow, xlCol) <> ""
If LH = True Then
Tbl = "Info.CaseBLH"
InsertQuery.CommandText = "INSERT INTO " & Tbl & " VALUES('"
ElseIf RH = True Then
Tbl = "Info.CaseBRH"
InsertQuery.CommandText = "INSERT INTO " & Tbl & " VALUES('"
Else
MsgBox ("No available sheets")
'Application.Quit
End If
NK21Data.TableDefs(Tbl).Fields.Count
For a = 1 To Fields.Count - 1
'For xlCol = 119 To 230 'columns DO1 to HV1
Fields.Item(a) = Replace(Cells(xlRow, xlCol), "'", "''") & "', '" 'Includes mitigation for apostrophes in the data
If Cells(xlRow, xlCol) = "" Then
rst.Fields.Item(a) = "NULL"
End If
xlCol = xlCol + 1
Next a
a = a + 1
Fields.Item(a) = (Format(Now(), "M/D/YYYY") & "')" & vbCrLf)
Wend
'On Error GoTo ErrorHandler
DBconnection.Execute (InsertQuery.CommandText)
DBconnection.Close
Set DBconnection = Nothing
ErrorHandler:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
End Sub
The error I get is:
Command text was not set for the command object.
This error occurs at:
DBconnection.Execute (InsertQuery.CommandText)
If I try using the following:
InsertQuery = DBconnection.Execute
I will get the following error:
Argument not optional
I've been at this for about (give or take) three days and I'm now having nightmares about it so if someone can help me figure out what to do for this I would greatly appreciate it.
I fixed up and cleaned the code from my earlier answer, tested it to work:
Here's the code:
Option Explicit
Sub DoItThen()
Dim i As Integer, sqlIns As String, sqlVals As String
Dim InsertQuery As New ADODB.Command
Dim firstRow As Long, firstCol As Integer, lastCol As Integer, currRow As Integer
Dim DBconnection As New ADODB.Connection
Dim ConnString As String
ConnString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Example;Data Source=MYMACHINENAME"
DBconnection.Open ConnString
InsertQuery.ActiveConnection = DBconnection
InsertQuery.CommandType = adCmdText
''build the command text side by side, named columns and values with param placeholders
sqlIns = "INSERT INTO person("
sqlVals = " VALUES("
''i could work these out by scanning the sheet i suppose. hardcoded for now
firstRow = 2
firstCol = 3
lastCol = 5
''generate the SQL - its this that lets the column names come in any order in the sheet
For i = firstCol To lastCol
sqlIns = sqlIns & Cells(firstRow, i) & ","
sqlVals = sqlVals & "?,"
InsertQuery.Parameters.Append InsertQuery.CreateParameter("p" & i - firstCol, adVarChar, adParamInput, 255)
Next i
''chop off the extra trailing commas and form a syntax correct command
InsertQuery.CommandText = Left$(sqlIns, Len(sqlIns) - 1) & ")" & Left$(sqlVals, Len(sqlVals) - 1) & ")"
''iterate the data part of the sheet and execute the query repeatedlty
currRow = firstRow + 1
While Cells(currRow, firstCol) <> ""
For i = firstCol To lastCol
InsertQuery.Parameters("p" & i - firstCol).Value = Cells(currRow, i)
Next i
InsertQuery.Execute , , adExecuteNoRecords ''dont return a resultset
currRow = currRow + 1
Wend
DBconnection.Close
Set DBconnection = Nothing
ErrorHandler:
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End Sub
It takes the first row as the names of the columns in the db table - any order is fine
It builds a command and populates the parameters
It repeatedly fills the values and executes the query, populating the table
OK; don't shoot me - I'm no VBA whizz but I'm saying you should strive to make your code more like this:
Sub DoItThen()
Dim a As Integer, sql as String
Dim InsertQuery As New ADODB.Command
Dim xlRow As Long, xlCol As Integer
Dim DBconnection As New ADODB.Connection
Dim ConnString As String
ConnString = "Provider=sqloledb;Server="";Inital Catalog="";Integrated Security=SSPI;User ID="";Password="""
DBconnection.Open ConnString
InsertQuery.ActiveConnection = conn
InsertQuery.CommandType = adCmdText
If LH = True Then
sql = "INSERT INTO Info.CaseBLH VALUES(#p1"
ElseIf RH = True Then
sql = "INSERT INTO Info.CaseBRH VALUES(#p1"
Else
MsgBox ("No available sheets")
'Application.Quit
End If
''does this do anything? I don't know
NK21Data.TableDefs(Tbl).Fields.Count
''let us add some placeholders to the command: we add count-2 because we already have one ? in the command
''ps; don't know where you got fields.count from
For a = 2 To Fields.Count - 1
sql = sql & ",#p" & a
Next a
''finish off our command
InsertQuery.CommandText = sql & ")"
''now we have a command like INSERT INTO tbl VALUES(#p1, #p2, #p3.."
''and setting the command text might pre-populate the parameters collection
''with the same number of parameters as are in the command, so let's clear it and
''add the parameters again ourselves so we can control the type
InsertQuery.Parameters.Clear
''create a load of parameters
For a = 1 To Fields.Count - 1
InsertQuery.Parameters.Append InsertQuery.CreateParameter("#p" & a, adVarChar, adParamInput, 255) 'adjust if you have strings longer than 255
Next a
''Now all the parameters are set etc, we just go through all the rows,
''and all the columns and set the values, then execute the command, then change the values and execute again
''--> set the command up once and repeatedly execute it
xlRow = 1 'only one row being used *as of now*, and that is the top row in the excel sheet
xlCol = 119 'First column of misc. data
While Cells(xlRow, xlCol) <> ""
For a = 1 To Fields.Count - 1
InsertQuery.Parameters("#p" & a).Value = Cells(xlRow, xlCol + a)
Next a
InsertQuery.Execute , , adExecuteNoRecords ''dont return a resultset
Wend
DBconnection.Close
Set DBconnection = Nothing
ErrorHandler:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
End Sub
You have 100 columns and 1000 rows to insert from an excel sheet:
You set up the command, INSERT INTO ... VALUES (... 100 #parameter placeholders...)
You clear the parameters collection (in case VBA has decided to 'help' by creating them) and add a load of parameters that represent your strings in your sheet
You then iterate the sheet, row by row, setting each column value on the row, into the relevant parameter and after you set all the columns (100 times), you execute the command then move to the next row, set the values again and execute again (1000 times)
I've got absolutely no way of testing this, sorry - it's my best guess but I fully expect it still has some error because I don't really know where you got Fields from. The answer with 8 votes from here was helpful: VBA, ADO.Connection and query parameters - I distinctly recall from when I was using VB6 about 20 years ago, that ADODB would try and prepopulate the parameters collection in certain circumstances, with its guesses at the parameter types; we routinely cleared it and added our own, but you might have some success proceeding with the default parameters it makes
The names of the parameters are not relevant; only the position. There's no requirement that #p1 from the query string matches the #p1 name given for the parameter - if the first parameter in the string were called #bob and you then cleared and added a parameter named #alice, whatever #alice's value was would be assigned to #bob because #bob is first in the query and #alice is first in the parameters collection. I used #pXXX as a parameter name for ease of reference in both cases
Here is my basic ADODB Execute template. This isn't meant to be an answer but more a helpful post. It should assist in showing you what you're doing incorrectly, which appears to be simple syntax issues as well as being really new to this (formatting and other pieces of code suggest that maybe you've gotten yourself "googled into a corner.").
Private Sub ADODBExample()
Dim vbSql As String, cnnstr as string
Dim cnn As ADODB.Connection
vbSql = "sql statement ;"
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB;Data Source=SERVERNAME;Initial Catalog=DBNAME;User ID=USERID;Password=PASSWORD; Trusted_Connection=No"
cnn.Open cnnstr
cnn.Execute vbSql
cnn.Close
Set cnn = Nothing
End Sub
More helpful tips -
Stop looping through cells, ranges and other worksheet/book objects. Learn to use arrays - itll make processing way better.
Simplicity is best. You appear to doing what I consider alot of unnecessary things, but then again I dont know all the requirements.
So I amended the code to the following:
Sub Connection()
Dim i As Integer, sqlIns As String, sqlVals As String
Dim InsertQuery As New ADODB.Command
Dim firstRow As Long, firstCol As Integer, lastCol As Integer, currRow As Integer
Dim DBconnection As New ADODB.Connection
Dim ConnString As String
Dim Tbl As String
ConnString = "Provider=sqloledb;Server=SERVER;Inital Catalog=DB;Integrated Security=SSPI;User ID=ID;Password=PW;"
DBconnection.Open ConnString
InsertQuery.ActiveConnection = DBconnection
InsertQuery.CommandType = adCmdText
If LH = True Then
Tbl = "Info.CaseBLH"
sqlIns = "INSERT INTO Info.CaseBLH("
ElseIf RH = True Then
Tbl = "Info.CaseBRH"
sqlIns = "INSERT INTO Info.CaseBRH("
Else
MsgBox ("No available sheets")
'Application.Quit
End If
''build the command text side by side, named columns and values with param placeholders
sqlVals = " VALUES("
''i could work these out by scanning the sheet i suppose. hardcoded for now
firstRow = 1
firstCol = 119
lastCol = 231
''generate the SQL - its this that lets the column names come in any order in the sheet
For i = firstCol To lastCol
sqlIns = sqlIns & Cells(firstRow, i) & ","
sqlVals = sqlVals & "?,"
InsertQuery.Parameters.Append InsertQuery.CreateParameter("p" & i - firstCol, adVarChar, adParamInput, 255)
Next i
''chop off the extra trailing commas and form a syntax correct command
InsertQuery.CommandText = Left$(sqlIns, Len(sqlIns) - 1) & ")" & Left$(sqlVals, Len(sqlVals) - 1) & ")"
''iterate the data part of the sheet and execute the query repeatedlty
currRow = firstRow ' - not needed as the data is automatically replaced with the code above
While Cells(currRow, firstCol) <> ""
For i = firstCol To lastCol - 1
InsertQuery.Parameters("p" & i - firstCol).Value = Cells(currRow, i)
Next i
InsertQuery.Execute , , adExecuteNoRecords ''dont return a resultset
Wend
DBconnection.Close
Set DBconnection = Nothing
ErrorHandler:
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End Sub
Right at the
InsertQuery.Execute , , adExecuteNoRecords
Line I'm getting a error telling me there is a syntax error around the ':' which doesn't make any sense to me. If I append my code to send the error to the error handler, every single row it cycles through throws me an error saying there is a syntax error around '-' or '/'. I think it has something to do with the parameter.value line.

Extracting data from over a million records

I have an Excel file in which I have set up a connection with an Access database. In the Excel file I have a list of names in column A, and I want to search these names in the Access database and return back two fields from that database. I need to do this for around 200-300 names.
Here is my code:
N = Cells(Rows.Count, "A").End(xlUp).Row
Application.DisplayAlerts = False
strDB = ThisWorkbook.Path & "file.accdb"
Set objConnection = New ADODB.Connection
objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strDB
For i = 2 To N
Dim rstTable As ADODB.Recordset
Set rstTable = New ADODB.Recordset
lookup = Range("A" & i).Value
strSQL = "SELECT NAME1,NAME2 FROM DATA WHERE [Field2]= """ & lookup & """;"
'Store query output
rstTable.Open Source:=strSQL, ActiveConnection:=objConnection
'Paste results to Transactions sheet
Worksheets("Sheet1").Range("B" & i).CopyFromRecordset rstTable
'Close the record set & connection
rstTable.Close
objConnection.Close
Next i
This works (kindof) but it takes an extremely long time and randomly crashes. Any ideas how to improve this?
Making sure there is a key on the lookup field will help. I would suggest making a copy of the workbook and test external data from Access or MS Query to see if that gives a performance gain over VBA.
When using MS Query or data from Access, you can modify the command text in the connection properties and use ? in the where clause to specify the parameter in the worksheet (so you don't lose that functionality).
I modified your SQL statement. Replace the Where [Field2] = "xxx" by Where [Field2] IN ("xxx", "yyy", "zzz").
N = Cells(Rows.Count, "A").End(xlUp).Row
Application.DisplayAlerts = False
strDB = ThisWorkbook.Path & "file.accdb"
Set objConnection = New ADODB.Connection
objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strDB
For i = 2 To N
lookup = lookup & "'" & Range("A" & i).Value & "', "
Next i
lookup = left(lookup, len(lookup) - 2)
Dim rstTable As ADODB.Recordset
Set rstTable = New ADODB.Recordset
strSQL = "SELECT NAME1,NAME2 FROM DATA WHERE [Field2] IN (" & lookup & ");"
'Store query output
rstTable.Open Source:=strSQL, ActiveConnection:=objConnection
'Paste results to Transactions sheet
Worksheets("Sheet1").Range("B" & i).CopyFromRecordset rstTable
'Close the record set & connection
rstTable.Close
objConnection.Close
You close the connection after the first iteration, so your next iteration -- which does not have code to open the connection -- would fail. So you should move the objConnection.Close out of the loop.
But, even then, to execute the same kind of query over and over again, just with a different argument, can be done in one go, using the IN (...) syntax:
' Declare all your variables
Dim N As Long
Dim strDB As String
Dim objConnection As ADODB.Connection
Dim rstTable As ADODB.Recordset
Dim strSQL As String
N = Cells(Rows.Count, "A").End(xlUp).Row
Application.DisplayAlerts = False
strDB = ThisWorkbook.Path & "file.accdb"
Set objConnection = New ADODB.Connection
objConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strDB
' collect the values in comma-separated string
lookup = ""
For i = 2 To N
lookup = lookup & ",""" & Range("A" & i).Value & """"
Next i
' Chop off the first comma
lookup = Mid(lookup, 2)
' Perform a single query, but also select the Field2 value
Set rstTable = New ADODB.Recordset
strSQL = "SELECT Field2, NAME1,NAME2 FROM DATA WHERE [Field2] IN (" & lookup & ");"
' query output
rstTable.Open Source:=strSQL, ActiveConnection:=objConnection
' Retrieve values
While Not rstTable.EOF
lookup = rstTable.Fields(0).Value
' Locate in which row to put the result
For i = 2 To N
If lookup = Range("A" & i).Value Then
Range("B" & i).Value = rstTable.Fields(1).Value
Range("C" & i).Value = rstTable.Fields(2).Value
End If
Next i
rstTable.MoveNext
Loop
' Close the record set & connection
rstTable.Close
objConnection.Close
You can do what you described, but I think it's far more efficient to do this in Access itself. Just create a table with your names and do an Inner Join to the table you want to find 2 fields. Should take less than a minute, and probably less than 30 seconds.

Adding unique data from Source Worksheet to Master Worksheet

A row from the Source List Worksheet (SLW) columns (1, 2 & 3) needs pasted into the Master List Worksheet (MLW) columns (3, 4 & 5) [same order] if the unique ID number (SLW1 = MLW3) does NOT already exists in the "Master List" (same workbook).
My first Excel VBA project ever. So any and all advice/suggestions/corrections/short cuts would be great. This code is what I have fumbled creating. As you know, its not working.
Sub Transfer()
Dim SLR As Integer 'SourceList's Woksheets Last Row
Dim MLR As Integer 'MasterList's Woksheets Last Row
Dim SC As Integer 'SourceList Counting through the loop (ROW NUMBER)
Dim SR As Range 'SourceList A-C Row data
'(Source information 3 rows to be transfered)
Dim ID As Integer 'Unique code of Projects
Dim Found As Range
Sheets("SourceList").Activate
SLR = Cells(Rows.Count, "A").End(xlUp).Row
'Start loop to go through SourceList unique ID numbers
For SC = 2 To SLR
'Copy SourceList ID number into Variable "ID"
ID = Sheets("SourceList").Range(1, SC)
'Also, Save Range into Variable so it doesn't have to
'go back and forth between Worksheets
Set SR = Range(Cells(1, SC), Cells(3, SC))
Sheets("MasterList").Activate
Found = Columns("C:C").Find(What:=ID, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Found Is Nothing Then
MLR = Cells(Rows.Count, "C").End(xlUp).Row + 1
Range(Cells(3, MLR)) = SR
SR.ClearContents
End If
Sheets("SourceList").Activate
Next SC
End Sub
Although I've posted a link for you to check out, I will post this solution which I've used before.
Sub ject()
Dim con As Object: Set con = CreateObject("ADODB.Connection")
Dim rec As Object: Set rec = CreateObject("ADODB.Recordset")
Dim datasource As String
datasource = ThisWorkbook.FullName ' returns the fullpath
Dim sconnect As String
sconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & datasource & ";" & _
"Extended Properties=""Excel 12.0;HDR=YES"";"
con.Open sconnect
Dim sqlstr As String
' This basically executes anti-join if you know SQL
sqlstr = "SELECT * "
sqlstr = sqlstr & "FROM [SWL$] e "
sqlstr = sqlstr & "LEFT JOIN [MWL$] u "
sqlstr = sqlstr & "ON e.ID = u.ID "
sqlstr = sqlstr & "WHERE u.ID IS NULL "
sqlstr = sqlstr & "AND e.ID IS NOT NULL;"
rec.Open sqlstr, con, 3, 1
' Dump data that meets your requirement
With Sheets("MWL")
Dim lr As Long
lr = .Range("D" & .Rows.Count).End(xlUp).Row + 1
.Range("D" & lr).CopyFromRecordset rec
End With
End Sub
Considerations:
Your SWL and MWL sheet data should start at Row 1 with headers.
Both should have the header name ID which contains the unique identifier. If not, you can adjust the code above.
So what the code does is access ADO (Active Data Objects) to be able to execute data comparison using SQL commands. It is way faster than the conventional Range to Range comparison (looping). I'm not sure if it is faster than Array to Array comparison but it is certainly easier to read and adjust once you get the hang of it. Anyways, this maybe a little bit too much at the moment (since you said it is your first project), but this is tried and tested and certainly works.
IMPORTANT: Notice the sconnect variable. You need to use the correct Connection String depending on the version of your Excel.

Looking up Access database in Excel

I want to do something very simple: I have an Access database with one table mapping thousands of product IDs to product information fields. In an Excel worksheet, the user types in perhaps 100 product IDs in the first column. I need for the remaining columns to pull in information from the Access database for the corresponding IDs. Specifically:
if I use MS-Query, it seems to insist on the output being a table. I simply want the output to be inside a single cell. Preferably, a formula that involves a SQL-type query.
I don't want any of the values to be updated automatically, but rather want all the columns updated only on user demand (the user could either choose refresh through a menu, or a VBA-based refresh button on the sheet is fine as well).
I'm thinking this would be a straightforward use case, but it seems surprisingly hard to find a solution. Thank you in advance!
Working from Excel, you can use ADO to connect to a database. For Access and Excel 2007/2010, you might:
''Reference: Microsoft ActiveX Data Objects x.x Library
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
''Not the best way to refer to a workbook, but convenient for
''testing. it is probably best to refer to the workbook by name.
strFile = ActiveWorkbook.FullName
''Connection string for 2007/2010
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0 xml;HDR=Yes;"";"
cn.Open strCon
''In-line connection string for MS Access
scn = "[;DATABASE=Z:\Docs\Test.accdb]"
''SQL query string
sSQL = "SELECT a.Stuff, b.ID, b.AText FROM [Sheet5$] a " _
& "INNER JOIN " & scn & ".table1 b " _
& "ON a.Stuff = b.AText"
rs.Open sSQL, cn
''Write returned recordset to a worksheet
ActiveWorkbook.Sheets("Sheet7").Cells(1, 1).CopyFromRecordset rs
Another possibility returns a single field from MS Access. This example uses late binding, so you do not need a library reference.
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer
strFile = "z:\docs\test.accdb"
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile
''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
''Select a field based on a numeric reference
strSQL = "SELECT AText " _
& "FROM Table1 a " _
& "WHERE ID = " & Sheets("Sheet7").[A1]
rs.Open strSQL, cn, 3, 3
Sheets("Sheet7").[B1] = rs!AText
OK, this may seem a bit lengthy - Create an Excel-table - in the first row (from column two) you have the Fieldnames Exactly as you have them in the access-table, in the first column you have the desired key-values (e.g. CustomerIDs).
When you run the macro it fills in what it finds...
Sub RefreshData()
Const fldNameCol = 2 'the column with the first fieldname in it'
Dim db, rst As Object
Set db = DBEngine.workspaces(0).OpenDatabase("C:\path\to\db\name.accdb")
Set rst = db.openrecordset("myDBTable", dbOpenDynaset)
Dim rng As Range
Dim showfields() As Integer
Dim i, aRow, aCol As Integer
ReDim showfields(100)
Set rng = Me.Cells
aRow = 1 'if you have the fieldnames in the first row'
aCol = fldNameCol
'***** remove both '' to speed things up'
'On Error GoTo ExitRefreshData'
'Application.ScreenUpdating = False'
'***** Get Fieldnames from Excel Sheet'
Do
For i = 0 To rst.fields.Count - 1
If rst.fields(i).Name = rng(aRow, aCol).Value Then
showfields(aCol) = i + 1
Exit For
End If
Next
aCol = aCol + 1
Loop Until IsEmpty(rng(aRow, aCol).Value)
ReDim Preserve showfields(aCol - 1)
'**** Get Data From Databasetable'
aRow = 2 'startin in the second row'
aCol = 1 'key values (ID) are in the first column of the excel sheet'
Do
rst.FindFirst "ID =" & CStr(rng(aRow, aCol).Value) 'Replace ID with the name of the key field'
If Not rst.NoMatch Then
For i = fldNameCol To UBound(showfields)
If showfields(i) > 0 Then
rng(aRow, i).Value = rst.fields(showfields(i) - 1).Value
End If
Next
End If
aRow = aRow + 1
Loop Until IsEmpty(rng(aRow, aCol).Value)
ExitRefreshData:
Application.ScreenUpdating = True
On Error GoTo 0
End Sub
And if you dont want your fieldnames in the excel sheet replace the paragraph "Get Fieldnames From Excelsheet" with this:
fieldnames = Split("field1name", "", "", "field3name")
For j = 0 To UBound(fieldnames) - 1
For i = 0 To rst.fields.Count - 1
If rst.fields(i).Name = fieldnames(j) Then
showfields(j + fldNameCol) = i + 1
Exit For
End If
Next
Next
ReDim Preserve showfields(UBound(fieldnames) - 1 + fldNameCol)
and add this at the top
dim j as integer
dim fieldnames