heatherjean
New Member
- Joined
- Sep 25, 2017
- Messages
- 3
Hello - Below is the VBA Macro with the SQL. The input box is prompting me, but not producing a result. Please help update my code if possible:
Sub SO()
'
' SO Macro
'
'
'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
Dim myVar As Long
'Setup the connection string for accessing MS SQL database
ConnectionString = "DSN=SQL M2M 2;Description=M2M7.0;UID=hunrast;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=ADMIN-PC1;DATABASE=M2MDATA01"
'Opens connection to the database
cnn.Open ConnectionString
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900
'This is your actual MS SQL query that you need to run; you should check this query first using a more robust SQL editor (such as HeidiSQL) to ensure your query is valid
StrQuery = "select jomast.fjobno, jomast.fsono, jodbom.fbompart, jodbom.fbomrev, jodbom.fbomsource, jodbom.fpono, jodbom.fpoqty, jodbom.ftotqty, poitem.fordqty, poitem.frcpqty from dbo.jomast jomast, {oj dbo.jodbom jodbom left outer join dbo.poitem poitem on jodbom.fbompart = poitem.fpartno and jodbom.fbomrev = poitem.frev and jodbom.fjobno = poitem.fjokey } where jomast.fjobno = jodbom.fjobno and ((jomast.fsono = '" & myVar & "' and jodbom.fbomsource in ('B','S'))) order by jomast.fjobno, jodbom.fbompart, jodbom.fbomrev"
'Performs the actual query
rst.Open StrQuery, cnn
myVar = InputBox("Enter the SO", "Example: 036978")
'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
Worksheets("Query").Range("A4").CopyFromRecordset rst
End Sub
Sub SO()
'
' SO Macro
'
'
'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
Dim myVar As Long
'Setup the connection string for accessing MS SQL database
ConnectionString = "DSN=SQL M2M 2;Description=M2M7.0;UID=hunrast;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=ADMIN-PC1;DATABASE=M2MDATA01"
'Opens connection to the database
cnn.Open ConnectionString
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900
'This is your actual MS SQL query that you need to run; you should check this query first using a more robust SQL editor (such as HeidiSQL) to ensure your query is valid
StrQuery = "select jomast.fjobno, jomast.fsono, jodbom.fbompart, jodbom.fbomrev, jodbom.fbomsource, jodbom.fpono, jodbom.fpoqty, jodbom.ftotqty, poitem.fordqty, poitem.frcpqty from dbo.jomast jomast, {oj dbo.jodbom jodbom left outer join dbo.poitem poitem on jodbom.fbompart = poitem.fpartno and jodbom.fbomrev = poitem.frev and jodbom.fjobno = poitem.fjokey } where jomast.fjobno = jodbom.fjobno and ((jomast.fsono = '" & myVar & "' and jodbom.fbomsource in ('B','S'))) order by jomast.fjobno, jodbom.fbompart, jodbom.fbomrev"
'Performs the actual query
rst.Open StrQuery, cnn
myVar = InputBox("Enter the SO", "Example: 036978")
'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
Worksheets("Query").Range("A4").CopyFromRecordset rst
End Sub