Good afternoon, all.
We have several Excel tools that we use daily here at work. Most of them were built by someone who has not worked here in years. I have been tasked with learning the in's and out's of VBA and Excel to help maintain and modify these tools as needed.
Out of necessity, my approach has been to work until I hit a snag, then study my way out of it.
Most of these tools draw data from the "sourcefile" listed below. If the workbook is drawing info on one single job, it goes to the Excel files generated based on the data in the .DBC file. If, however, the workbook is dealing with
several jobs, it uses the ODBC and SQL format illustrated in the example below.
I have tried to modify this code as well as create my own. I have had some occasional dumb luck and stumbled across something that "works"(?) but I can hardly claim to understand it. I have been able to replicate this result by
using the Get External Data - From Other Sources - From MS Query, but what I wind up with is a table on an additional sheet that I must then copy and paste into my target worksheet.
My question is this....is there something that helped him write this code in the macro, itself? Is there a way I can find this kind of "source code" for the queries I have generated using MS Query? Do I just need to learn the language
of SQL in order to code this in my macros? I know, that's three questions, but really just one, ultimately...how do I go from the Query wizard to the embedded code shown below?
Thanks for your time and attention.
sourcefile = "E:\jobwin\DATA1.DBC"
connstring = _
"ODBC;Driver={Microsoft Visual FoxPro Driver};SourceDB=" & sourcefile & ";SourceType=DBC;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;"
Set Target = Range("A5")
jobnumber = Target.Value
With Target.Offset(0, 0)
sqlstring01 = "SELECT jobent.jobno, jobent.ponum, jobent.lineitem, jobent.partno, space(0) as 'Notes', purchase.purnumb, purchase.due_date as 'Due Date from Vendor', purchase.vendno as
'location', "
sqlstring02 = "jobent.custno, jobent.qty - jobent.qtyship as 'qty dif', jobent.descrip, jobent.qtyship, jobent.duedate, jobent.certcomp, "
sqlstring03 = "jobent.sourceapp, jobent.dteship, space(0) as 'Main Job', jobent.rem1 as 'mainjob source', "
sqlstring04 = "space(0) as 'adjusted due date', jobent.qty, "
sqlstring05 = "purchase.ordqty as 'vendor ordqty', purchase.qtylog as 'vendor qty rcv', "
sqlstring06 = "space(0) as 'repeat vendor PO', jobent.duedate < DATE() as 'late?' "
sqlstring07 = "from purchase right join jobent "
sqlstring08 = "on (jobent.jobno = purchase.jobno and jobent.qtyship < jobent.qty and purchase.qtylog < purchase.ordqty and purchase.pdate>{d '1900-01-01'}) "
sqlstring09 = "where (jobent.qtyship < jobent.qty and jobent.duedate < {d '9999-01-01'}) "
sqlstring10 = "order by jobent.duedate, jobent.jobno, purchase.due_date, purchase.purnumb"
sqlstring = sqlstring01 & sqlstring02 & sqlstring03 & sqlstring04 & sqlstring05 & sqlstring06 & sqlstring07 & sqlstring08 & sqlstring09 & sqlstring10
With ActiveSheet.QueryTables.Add(Connection:=connstring, _
Destination:=Target.Offset(0, 0), Sql:=sqlstring)
.RefreshStyle = xlInsertDeleteCells
.FieldNames = False
.AdjustColumnWidth = False
.Refresh BackgroundQuery:=False
.SaveData = True
.Delete
End With
End With