Splitting Access database via VBA takes suddenly ages - vba

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

Related

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.

Importing values from excel to access with criteria from access table

First of all, thanks in advance for your time and help. Here is my situation;
I'm trying to add some values from an existing excel workbook into an existing table in an access database. Here is the code I've found and played with it a little according to my needs, but when I click on the button, it does nothing. No error messages, no imported values, just does nothing.
Private Sub Command39_Click()
On Error GoTo Err_Handler
Dim wbFDU As Workbook
Dim objExcelApp As Excel.Application
Dim db As Database
Dim rstWL As Recordset
Dim columnI As String
Dim columnS As String
Dim searchInC As String
Dim C As String
Dim I As String
Dim M As String
Dim S As String
Dim W As String
Dim iCounter As Integer
Set db = CurrentDb
Set rstWL = db.OpenRecordset("Select * FROM WL WHERE WLDate = Date()-1")
Set objExcelApp = New Excel.Application
objExcelApp.Workbooks.Open ("\\CDB\Shared\MNG\CIO Mng.xlsm")
Set wbFDU = objExcelApp.Workbooks(1)
If rstWL.EOF = False Then
rstWL.MoveFirst
Do While rstWL.EOF = False
iCounter = 1
C = "C" & iCounter
I = "I" & iCounter
M = "M" & iCounter
S = "S" & iCounter
W = "W" & iCounter
Do Until wbFDU.Worksheets("Rep").Range(C).Value = ""
searchInC = wbFDU.Worksheets("Rep").Range(C).Value
If rstWL!CustName = searchInC Then
columnI = wbFDU.Worksheets("Rep").Range(I).Value + wbFDU.Worksheets("Rep").Range(M).Value
columnS = wbFDU.Worksheets("Rep").Range(S).Value + wbFDU.Worksheets("Rep").Range(W).Value
rstWL.Edit
rstWL.Fields("LCDCO") = columnI
rstWL.Update
rstWL.Fields("ECDCO") = columnS
rstWL.Update
End If
iCounter = iCounter + 1
C = "C" & iCounter
I = "I" & iCounter
M = "M" & iCounter
S = "S" & iCounter
W = "W" & iCounter
Loop
rstWL.MoveNext
Loop
End If
wbFDU.Close False
Set wbFDU = Nothing
rstWL.Close
Set rstWL = Nothing
db.Close
Set db = Nothing
Exit Sub
Err_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number " & Err.Number & vbCrLf & _
"Error Description" & Err.Description & vbCrLf & _
"Your application will close!", _
vbCritical, "An Error has Occured"
End Sub
Since couple of days cannot figure it out what I'm doing wrong.
Consider directly querying Excel workbook in MS Access without the need of recordset looping. Specifically, use a temp table re-created or cleaned out each time to use in UPDATE query with WL.
However, it seems your workbook does not use any column headers, so specify HDR=No which will result in F1, F2, F3, ... in query resultset. Otherwise, use HDR=Yes and reference named columns.
Make-Table Query (after first time, use delete/append query for subsequent Excel updates)
SELECT *
INTO myExcelTempTable
FROM [Excel 12.0 Xml;HDR=No;Database=\\CDB\Shared\MNG\CIO Mng.xlsm].[Rep$] AS t;
Update Query
UPDATE WL
INNER JOIN myExcelTempTable AS t
ON WL.CustomerId = t.F3 --F3 being Column C (customer id)
SET LCDCO = F9, --F9 being Column I
ECDCO = F19 --F19 being Column S
WHERE WL.WLDate = Date()-1;
Do not use comments in above query in MS Access. Only included here to guide you.

VB macro to identify different combinations of cells

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

Shift Excel Cell down after executing a macro for second time

I've written a (below)macro that pulls data from the sql server 2008 r2. My issue is when the user runs the macro for first time by entering Jobnumber (say J0001) excel puts data on the spreadsheet starting from cell "A1" which is fine. The issue here is, when the user runs the macro for the second time by entering the jobnumber (say J0002), excel puts the data for Jobnumber (J0002) on cell "A1" and shifts the cells for J0001(first job) to cell "F" instead of moving down. How can I shift the previous entry down in the spreadsheet with the latest entry on top?
Here is my macro and attachment:
Sub Task()
Dim sqlstring As String
Dim connstring As String
Dim Strcode As String
Strcode = Trim(InputBox("Please enter a Job #", "Task history"))
sqlstring = "select distinct m.JobNumber , cast(m.ExpectedDate as DATE) 'Ship Date' ,m.CustLongName 'Customer' & _
" from ArchiveJobHeader m left join AuxiliaryInfoFile af (nolock) on af.jobnumber=m.jobnumber & _
" where m.JobNumber = '" & Trim(Strcode) & "'" & _
" order by 'Resulttime'"
connstring = "ODBC;DSN=SQLDSN;UID=test;PWD=test123"
Dim thisQT As QueryTable
Set thisQT = ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("a1", "a1000"))
thisQT.BackgroundQuery = False
thisQT.Sql = sqlstring
thisQT.Refresh
End Sub][1]
If you incorporate a lastRow check and then assign a variable the Next Row number, you can concatenate your Range and it will be a new row every time.
Dim lastRow As Long, nextRow As Long
lastRow = Sheets("Sheet Name").Range("A" & Rows.count).End(xlUp).row
nextRow = lastRow + 1
Then when you set your Range, concatenate the variable with the string.
Set thisQT = ActiveSheet.QueryTables.Add( _
Connection:=connstring, _
Destination:=Range("A" & nextRow))
I'm not sure what you are doing with row 1000 as shown in your question. But this is the idea of using a variable with your normal Range Address.
You could have something like this:
Sub a()
'Must set Reference to "Microsoft ActiveX Data Objects 2.8 Library"
Dim ws As Worksheet
Dim n As Long ' Row To Write In
Dim sql As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Long
Set ws = ThisWorkbook.Worksheets("Tab Name")
'Assuming you already have Headings in row 1 _
and start adding records from "A2" down...
n = ws.Range("A10000").End(xlUp).row + 1
'you sql string above is missing some double quotes...
sql = "select distinct m.JobNumber, cast(m.ExpectedDate as DATE) 'Ship Date', m.CustLongName 'Customer'" & _
" from ArchiveJobHeader m left join AuxiliaryInfoFile af (nolock) on af.jobnumber=m.jobnumber" & _
" where m.JobNumber = '" & Trim(Strcode) & "'" & _
" order by 'Resulttime'"
Set cn = New ADODB.Connection
' using an ODBC DSN... as in <http://msdn.microsoft.com/en-us/library/ms807027.aspx>
cn.Open "SQLDSN", "test", "test123"
Set rs = cn.Execute(sql) ' likely, in your case, to return one record only, _
so you are on there right away
For i = 0 To rs.Fields.Count
ws.Cells(n, i + 1) = rs(i)
Next
rs.Close
cn.Close
End Sub
You would need to put more work into this, I am afraid, but this is the direction you may consider.

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