VBA Write dynamic query from an Excel workbook and .sql file





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















enter image description hereI have an XL file which has some data validation linked to a list of G/L accounts and Departments, as well as periods. The goal is for the user to select their choice, and for the .sql file to dynamically update the code and using ADO output the query to another workbook with the parameterized data. Unfortunately when I am doing the loop of the text stream, the .AtEndofText property is switching to true on blank lines of the .sql file and the loop dies there (line 9 , for example).



Is there an easier way to do this ? Ie. continue to loop through blank lines?



Or do I have to simply modify my .sql file to remove all blank lines?



Thank you



Option Explicit

Option Base 1

Private Sub Get_and_LoadData()

Dim S As Worksheet, LO As ListObject, Arr() As Variant, NB As Workbook
Dim B As Workbook
Dim fPath As String, FSO As FileSystemObject, sFile As TextStream
Dim sSQL As Variant, x As Long
Dim aConn As Object, aComm As Object, aRec As Object, ConnSTR As String
Dim fDir As String, tempFile As String
Dim nFile As TextStream

On Error GoTo sqlErr

'Set objects
fPath = "\silicavol11GroupsFinanceOps FinanceReportingF18 FinancialsLN_Data_LookupSQL_Pull Financial Data_V2.sql"
Set B = ThisWorkbook
Set FSO = New FileSystemObject
Set aConn = VBA.CreateObject("ADODB.Connection")
Set aComm = VBA.CreateObject("ADODB.Command")
Set aRec = VBA.CreateObject("ADODB.Recordset")
Set S = ShTitle
Set LO = S.ListObjects("ParameterTable")

'Array for Parameters (The "Set" Statements range)
Arr = LO.DataBodyRange.offset(, 10)

'Check for source file existence
If VBA.Dir$(fPath, vbNormal) = "" Then
MsgBox "No .Sql file!", vbExclamation, "Ensure file exists.."
Exit Sub
Else
fDir = B.Path & ""
End If

'Set file system objects
Set sFile = FSO.OpenTextFile(Filename:=fPath, IOMode:=ForReading, _
Create:=False)
tempFile = fDir & "temp" & VBA.Replace(Timer, ".", "") & ".txt"
Set nFile = FSO.CreateTextFile(Filename:=tempFile)

'Get SQL Script
'sSQL = sFile.ReadAll 'read file contents into a variable

'Update SQL SCRIPT
Do Until sFile.AtEndOfStream = True

'Loop through the lines of the text stream
Do Until sFile.AtEndOfLine = True

'Note these are the lines where the SET statements exist
If sFile.line >= 8 And sFile.line <= 14 Then

x = x + 1
nFile.WriteLine Arr(x, 2)
sFile.SkipLine 'to jump to next line

ElseIf sFile.line = 65 Or sFile.line = 66 Then 'comment out parameters not used

nFile.WriteLine "-- " & sFile.ReadLine

Else

nFile.WriteLine sFile.ReadLine 'Updates .Line property

End If

Loop
' x = 0

Loop

'Get connection string to DB
ConnSTR = getConnection(ConnSTR)

'Open connection to the Database
aConn.Open ConnSTR
aConn.defaultdatabase = "ln"

'Load the sql command into an object
With aComm
.ActiveConnection = ConnSTR
.CommandText = sSQL
.CommandTimeout = 300 '5 minute QRY execution max
End With

'Load the record Set
Set aRec = aComm.Execute(sSQL)

'Kill the temp .txt file
Kill tempFile

'Output the record set to new workbook
Set NB = Application.Workbooks.Add
NB.Sheets(1).Range("A1").CopyFromRecordset aRec

'Delete from MEMORY
Set nFile = Nothing
Set aConn = Nothing
Set aComm = Nothing
Set aRec = Nothing
Set sFile = Nothing
sSQL = vbNullString
Set FSO = Nothing
Set B = Nothing
Set NB = Nothing
Set LO = Nothing
Erase Arr
Set S = Nothing

Exit Sub

sqlErr:
MsgBox Err.Description, vbExclamation

End Sub


Function getConnection(conn As String) As String

'LN Connection
getConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=True;Initial Catalog=ln;Data Source=erpdbsvr1erpln;" & _
"Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;" & _
"Workstation ID=" & LCase(Environ("username")) & "-LT;Use Encryption for Data=False;" & _
"Tag with column collation when possible=False;Trusted_connection=yes;"

End Function









share|improve this question

























  • Not sure you need this loop - Do Until sFile.AtEndOfLine = True That's what's giving you problems with empty lines.

    – Tim Williams
    Nov 26 '18 at 19:27













  • @TimWilliams the thing is I need to change the string to base it of user parameters, so I wouldn't know how to do it via .readall

    – Mike Mirabelli
    Nov 26 '18 at 19:29






  • 1





    You already have the outer Do Until sFile.AtEndOfStream = True loop - just remove the inner AtEndOfLine loop

    – Tim Williams
    Nov 26 '18 at 19:33


















0















enter image description hereI have an XL file which has some data validation linked to a list of G/L accounts and Departments, as well as periods. The goal is for the user to select their choice, and for the .sql file to dynamically update the code and using ADO output the query to another workbook with the parameterized data. Unfortunately when I am doing the loop of the text stream, the .AtEndofText property is switching to true on blank lines of the .sql file and the loop dies there (line 9 , for example).



Is there an easier way to do this ? Ie. continue to loop through blank lines?



Or do I have to simply modify my .sql file to remove all blank lines?



Thank you



Option Explicit

Option Base 1

Private Sub Get_and_LoadData()

Dim S As Worksheet, LO As ListObject, Arr() As Variant, NB As Workbook
Dim B As Workbook
Dim fPath As String, FSO As FileSystemObject, sFile As TextStream
Dim sSQL As Variant, x As Long
Dim aConn As Object, aComm As Object, aRec As Object, ConnSTR As String
Dim fDir As String, tempFile As String
Dim nFile As TextStream

On Error GoTo sqlErr

'Set objects
fPath = "\silicavol11GroupsFinanceOps FinanceReportingF18 FinancialsLN_Data_LookupSQL_Pull Financial Data_V2.sql"
Set B = ThisWorkbook
Set FSO = New FileSystemObject
Set aConn = VBA.CreateObject("ADODB.Connection")
Set aComm = VBA.CreateObject("ADODB.Command")
Set aRec = VBA.CreateObject("ADODB.Recordset")
Set S = ShTitle
Set LO = S.ListObjects("ParameterTable")

'Array for Parameters (The "Set" Statements range)
Arr = LO.DataBodyRange.offset(, 10)

'Check for source file existence
If VBA.Dir$(fPath, vbNormal) = "" Then
MsgBox "No .Sql file!", vbExclamation, "Ensure file exists.."
Exit Sub
Else
fDir = B.Path & ""
End If

'Set file system objects
Set sFile = FSO.OpenTextFile(Filename:=fPath, IOMode:=ForReading, _
Create:=False)
tempFile = fDir & "temp" & VBA.Replace(Timer, ".", "") & ".txt"
Set nFile = FSO.CreateTextFile(Filename:=tempFile)

'Get SQL Script
'sSQL = sFile.ReadAll 'read file contents into a variable

'Update SQL SCRIPT
Do Until sFile.AtEndOfStream = True

'Loop through the lines of the text stream
Do Until sFile.AtEndOfLine = True

'Note these are the lines where the SET statements exist
If sFile.line >= 8 And sFile.line <= 14 Then

x = x + 1
nFile.WriteLine Arr(x, 2)
sFile.SkipLine 'to jump to next line

ElseIf sFile.line = 65 Or sFile.line = 66 Then 'comment out parameters not used

nFile.WriteLine "-- " & sFile.ReadLine

Else

nFile.WriteLine sFile.ReadLine 'Updates .Line property

End If

Loop
' x = 0

Loop

'Get connection string to DB
ConnSTR = getConnection(ConnSTR)

'Open connection to the Database
aConn.Open ConnSTR
aConn.defaultdatabase = "ln"

'Load the sql command into an object
With aComm
.ActiveConnection = ConnSTR
.CommandText = sSQL
.CommandTimeout = 300 '5 minute QRY execution max
End With

'Load the record Set
Set aRec = aComm.Execute(sSQL)

'Kill the temp .txt file
Kill tempFile

'Output the record set to new workbook
Set NB = Application.Workbooks.Add
NB.Sheets(1).Range("A1").CopyFromRecordset aRec

'Delete from MEMORY
Set nFile = Nothing
Set aConn = Nothing
Set aComm = Nothing
Set aRec = Nothing
Set sFile = Nothing
sSQL = vbNullString
Set FSO = Nothing
Set B = Nothing
Set NB = Nothing
Set LO = Nothing
Erase Arr
Set S = Nothing

Exit Sub

sqlErr:
MsgBox Err.Description, vbExclamation

End Sub


Function getConnection(conn As String) As String

'LN Connection
getConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=True;Initial Catalog=ln;Data Source=erpdbsvr1erpln;" & _
"Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;" & _
"Workstation ID=" & LCase(Environ("username")) & "-LT;Use Encryption for Data=False;" & _
"Tag with column collation when possible=False;Trusted_connection=yes;"

End Function









share|improve this question

























  • Not sure you need this loop - Do Until sFile.AtEndOfLine = True That's what's giving you problems with empty lines.

    – Tim Williams
    Nov 26 '18 at 19:27













  • @TimWilliams the thing is I need to change the string to base it of user parameters, so I wouldn't know how to do it via .readall

    – Mike Mirabelli
    Nov 26 '18 at 19:29






  • 1





    You already have the outer Do Until sFile.AtEndOfStream = True loop - just remove the inner AtEndOfLine loop

    – Tim Williams
    Nov 26 '18 at 19:33














0












0








0








enter image description hereI have an XL file which has some data validation linked to a list of G/L accounts and Departments, as well as periods. The goal is for the user to select their choice, and for the .sql file to dynamically update the code and using ADO output the query to another workbook with the parameterized data. Unfortunately when I am doing the loop of the text stream, the .AtEndofText property is switching to true on blank lines of the .sql file and the loop dies there (line 9 , for example).



Is there an easier way to do this ? Ie. continue to loop through blank lines?



Or do I have to simply modify my .sql file to remove all blank lines?



Thank you



Option Explicit

Option Base 1

Private Sub Get_and_LoadData()

Dim S As Worksheet, LO As ListObject, Arr() As Variant, NB As Workbook
Dim B As Workbook
Dim fPath As String, FSO As FileSystemObject, sFile As TextStream
Dim sSQL As Variant, x As Long
Dim aConn As Object, aComm As Object, aRec As Object, ConnSTR As String
Dim fDir As String, tempFile As String
Dim nFile As TextStream

On Error GoTo sqlErr

'Set objects
fPath = "\silicavol11GroupsFinanceOps FinanceReportingF18 FinancialsLN_Data_LookupSQL_Pull Financial Data_V2.sql"
Set B = ThisWorkbook
Set FSO = New FileSystemObject
Set aConn = VBA.CreateObject("ADODB.Connection")
Set aComm = VBA.CreateObject("ADODB.Command")
Set aRec = VBA.CreateObject("ADODB.Recordset")
Set S = ShTitle
Set LO = S.ListObjects("ParameterTable")

'Array for Parameters (The "Set" Statements range)
Arr = LO.DataBodyRange.offset(, 10)

'Check for source file existence
If VBA.Dir$(fPath, vbNormal) = "" Then
MsgBox "No .Sql file!", vbExclamation, "Ensure file exists.."
Exit Sub
Else
fDir = B.Path & ""
End If

'Set file system objects
Set sFile = FSO.OpenTextFile(Filename:=fPath, IOMode:=ForReading, _
Create:=False)
tempFile = fDir & "temp" & VBA.Replace(Timer, ".", "") & ".txt"
Set nFile = FSO.CreateTextFile(Filename:=tempFile)

'Get SQL Script
'sSQL = sFile.ReadAll 'read file contents into a variable

'Update SQL SCRIPT
Do Until sFile.AtEndOfStream = True

'Loop through the lines of the text stream
Do Until sFile.AtEndOfLine = True

'Note these are the lines where the SET statements exist
If sFile.line >= 8 And sFile.line <= 14 Then

x = x + 1
nFile.WriteLine Arr(x, 2)
sFile.SkipLine 'to jump to next line

ElseIf sFile.line = 65 Or sFile.line = 66 Then 'comment out parameters not used

nFile.WriteLine "-- " & sFile.ReadLine

Else

nFile.WriteLine sFile.ReadLine 'Updates .Line property

End If

Loop
' x = 0

Loop

'Get connection string to DB
ConnSTR = getConnection(ConnSTR)

'Open connection to the Database
aConn.Open ConnSTR
aConn.defaultdatabase = "ln"

'Load the sql command into an object
With aComm
.ActiveConnection = ConnSTR
.CommandText = sSQL
.CommandTimeout = 300 '5 minute QRY execution max
End With

'Load the record Set
Set aRec = aComm.Execute(sSQL)

'Kill the temp .txt file
Kill tempFile

'Output the record set to new workbook
Set NB = Application.Workbooks.Add
NB.Sheets(1).Range("A1").CopyFromRecordset aRec

'Delete from MEMORY
Set nFile = Nothing
Set aConn = Nothing
Set aComm = Nothing
Set aRec = Nothing
Set sFile = Nothing
sSQL = vbNullString
Set FSO = Nothing
Set B = Nothing
Set NB = Nothing
Set LO = Nothing
Erase Arr
Set S = Nothing

Exit Sub

sqlErr:
MsgBox Err.Description, vbExclamation

End Sub


Function getConnection(conn As String) As String

'LN Connection
getConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=True;Initial Catalog=ln;Data Source=erpdbsvr1erpln;" & _
"Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;" & _
"Workstation ID=" & LCase(Environ("username")) & "-LT;Use Encryption for Data=False;" & _
"Tag with column collation when possible=False;Trusted_connection=yes;"

End Function









share|improve this question
















enter image description hereI have an XL file which has some data validation linked to a list of G/L accounts and Departments, as well as periods. The goal is for the user to select their choice, and for the .sql file to dynamically update the code and using ADO output the query to another workbook with the parameterized data. Unfortunately when I am doing the loop of the text stream, the .AtEndofText property is switching to true on blank lines of the .sql file and the loop dies there (line 9 , for example).



Is there an easier way to do this ? Ie. continue to loop through blank lines?



Or do I have to simply modify my .sql file to remove all blank lines?



Thank you



Option Explicit

Option Base 1

Private Sub Get_and_LoadData()

Dim S As Worksheet, LO As ListObject, Arr() As Variant, NB As Workbook
Dim B As Workbook
Dim fPath As String, FSO As FileSystemObject, sFile As TextStream
Dim sSQL As Variant, x As Long
Dim aConn As Object, aComm As Object, aRec As Object, ConnSTR As String
Dim fDir As String, tempFile As String
Dim nFile As TextStream

On Error GoTo sqlErr

'Set objects
fPath = "\silicavol11GroupsFinanceOps FinanceReportingF18 FinancialsLN_Data_LookupSQL_Pull Financial Data_V2.sql"
Set B = ThisWorkbook
Set FSO = New FileSystemObject
Set aConn = VBA.CreateObject("ADODB.Connection")
Set aComm = VBA.CreateObject("ADODB.Command")
Set aRec = VBA.CreateObject("ADODB.Recordset")
Set S = ShTitle
Set LO = S.ListObjects("ParameterTable")

'Array for Parameters (The "Set" Statements range)
Arr = LO.DataBodyRange.offset(, 10)

'Check for source file existence
If VBA.Dir$(fPath, vbNormal) = "" Then
MsgBox "No .Sql file!", vbExclamation, "Ensure file exists.."
Exit Sub
Else
fDir = B.Path & ""
End If

'Set file system objects
Set sFile = FSO.OpenTextFile(Filename:=fPath, IOMode:=ForReading, _
Create:=False)
tempFile = fDir & "temp" & VBA.Replace(Timer, ".", "") & ".txt"
Set nFile = FSO.CreateTextFile(Filename:=tempFile)

'Get SQL Script
'sSQL = sFile.ReadAll 'read file contents into a variable

'Update SQL SCRIPT
Do Until sFile.AtEndOfStream = True

'Loop through the lines of the text stream
Do Until sFile.AtEndOfLine = True

'Note these are the lines where the SET statements exist
If sFile.line >= 8 And sFile.line <= 14 Then

x = x + 1
nFile.WriteLine Arr(x, 2)
sFile.SkipLine 'to jump to next line

ElseIf sFile.line = 65 Or sFile.line = 66 Then 'comment out parameters not used

nFile.WriteLine "-- " & sFile.ReadLine

Else

nFile.WriteLine sFile.ReadLine 'Updates .Line property

End If

Loop
' x = 0

Loop

'Get connection string to DB
ConnSTR = getConnection(ConnSTR)

'Open connection to the Database
aConn.Open ConnSTR
aConn.defaultdatabase = "ln"

'Load the sql command into an object
With aComm
.ActiveConnection = ConnSTR
.CommandText = sSQL
.CommandTimeout = 300 '5 minute QRY execution max
End With

'Load the record Set
Set aRec = aComm.Execute(sSQL)

'Kill the temp .txt file
Kill tempFile

'Output the record set to new workbook
Set NB = Application.Workbooks.Add
NB.Sheets(1).Range("A1").CopyFromRecordset aRec

'Delete from MEMORY
Set nFile = Nothing
Set aConn = Nothing
Set aComm = Nothing
Set aRec = Nothing
Set sFile = Nothing
sSQL = vbNullString
Set FSO = Nothing
Set B = Nothing
Set NB = Nothing
Set LO = Nothing
Erase Arr
Set S = Nothing

Exit Sub

sqlErr:
MsgBox Err.Description, vbExclamation

End Sub


Function getConnection(conn As String) As String

'LN Connection
getConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=True;Initial Catalog=ln;Data Source=erpdbsvr1erpln;" & _
"Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;" & _
"Workstation ID=" & LCase(Environ("username")) & "-LT;Use Encryption for Data=False;" & _
"Tag with column collation when possible=False;Trusted_connection=yes;"

End Function






excel vba loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 19:28







Mike Mirabelli

















asked Nov 26 '18 at 19:22









Mike MirabelliMike Mirabelli

150110




150110













  • Not sure you need this loop - Do Until sFile.AtEndOfLine = True That's what's giving you problems with empty lines.

    – Tim Williams
    Nov 26 '18 at 19:27













  • @TimWilliams the thing is I need to change the string to base it of user parameters, so I wouldn't know how to do it via .readall

    – Mike Mirabelli
    Nov 26 '18 at 19:29






  • 1





    You already have the outer Do Until sFile.AtEndOfStream = True loop - just remove the inner AtEndOfLine loop

    – Tim Williams
    Nov 26 '18 at 19:33



















  • Not sure you need this loop - Do Until sFile.AtEndOfLine = True That's what's giving you problems with empty lines.

    – Tim Williams
    Nov 26 '18 at 19:27













  • @TimWilliams the thing is I need to change the string to base it of user parameters, so I wouldn't know how to do it via .readall

    – Mike Mirabelli
    Nov 26 '18 at 19:29






  • 1





    You already have the outer Do Until sFile.AtEndOfStream = True loop - just remove the inner AtEndOfLine loop

    – Tim Williams
    Nov 26 '18 at 19:33

















Not sure you need this loop - Do Until sFile.AtEndOfLine = True That's what's giving you problems with empty lines.

– Tim Williams
Nov 26 '18 at 19:27







Not sure you need this loop - Do Until sFile.AtEndOfLine = True That's what's giving you problems with empty lines.

– Tim Williams
Nov 26 '18 at 19:27















@TimWilliams the thing is I need to change the string to base it of user parameters, so I wouldn't know how to do it via .readall

– Mike Mirabelli
Nov 26 '18 at 19:29





@TimWilliams the thing is I need to change the string to base it of user parameters, so I wouldn't know how to do it via .readall

– Mike Mirabelli
Nov 26 '18 at 19:29




1




1





You already have the outer Do Until sFile.AtEndOfStream = True loop - just remove the inner AtEndOfLine loop

– Tim Williams
Nov 26 '18 at 19:33





You already have the outer Do Until sFile.AtEndOfStream = True loop - just remove the inner AtEndOfLine loop

– Tim Williams
Nov 26 '18 at 19:33












1 Answer
1






active

oldest

votes


















1














Something more like this (following from my comments above)



Dim inLine
'...
'...
Do Until sFile.AtEndOfStream = True
inLine = sFile.ReadLine
If sFile.Line >= 8 And sFile.Line <= 14 Then
x = x + 1
nFile.WriteLine Arr(x, 2)
ElseIf sFile.Line = 65 Or sFile.Line = 66 Then
nFile.WriteLine "-- " & inLine 'comment out parameters not used
Else
nFile.WriteLine inLine
End If
Loop





share|improve this answer
























  • This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

    – Mike Mirabelli
    Nov 26 '18 at 20:46














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487722%2fvba-write-dynamic-query-from-an-excel-workbook-and-sql-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Something more like this (following from my comments above)



Dim inLine
'...
'...
Do Until sFile.AtEndOfStream = True
inLine = sFile.ReadLine
If sFile.Line >= 8 And sFile.Line <= 14 Then
x = x + 1
nFile.WriteLine Arr(x, 2)
ElseIf sFile.Line = 65 Or sFile.Line = 66 Then
nFile.WriteLine "-- " & inLine 'comment out parameters not used
Else
nFile.WriteLine inLine
End If
Loop





share|improve this answer
























  • This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

    – Mike Mirabelli
    Nov 26 '18 at 20:46


















1














Something more like this (following from my comments above)



Dim inLine
'...
'...
Do Until sFile.AtEndOfStream = True
inLine = sFile.ReadLine
If sFile.Line >= 8 And sFile.Line <= 14 Then
x = x + 1
nFile.WriteLine Arr(x, 2)
ElseIf sFile.Line = 65 Or sFile.Line = 66 Then
nFile.WriteLine "-- " & inLine 'comment out parameters not used
Else
nFile.WriteLine inLine
End If
Loop





share|improve this answer
























  • This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

    – Mike Mirabelli
    Nov 26 '18 at 20:46
















1












1








1







Something more like this (following from my comments above)



Dim inLine
'...
'...
Do Until sFile.AtEndOfStream = True
inLine = sFile.ReadLine
If sFile.Line >= 8 And sFile.Line <= 14 Then
x = x + 1
nFile.WriteLine Arr(x, 2)
ElseIf sFile.Line = 65 Or sFile.Line = 66 Then
nFile.WriteLine "-- " & inLine 'comment out parameters not used
Else
nFile.WriteLine inLine
End If
Loop





share|improve this answer













Something more like this (following from my comments above)



Dim inLine
'...
'...
Do Until sFile.AtEndOfStream = True
inLine = sFile.ReadLine
If sFile.Line >= 8 And sFile.Line <= 14 Then
x = x + 1
nFile.WriteLine Arr(x, 2)
ElseIf sFile.Line = 65 Or sFile.Line = 66 Then
nFile.WriteLine "-- " & inLine 'comment out parameters not used
Else
nFile.WriteLine inLine
End If
Loop






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 19:39









Tim WilliamsTim Williams

89.4k97087




89.4k97087













  • This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

    – Mike Mirabelli
    Nov 26 '18 at 20:46





















  • This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

    – Mike Mirabelli
    Nov 26 '18 at 20:46



















This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

– Mike Mirabelli
Nov 26 '18 at 20:46







This worked thanks. Now I understand why it was stopping. Still learning to navigate the FSO. It's pretty powerful. Thanks Tim.

– Mike Mirabelli
Nov 26 '18 at 20:46






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487722%2fvba-write-dynamic-query-from-an-excel-workbook-and-sql-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen