If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > SQLInsert code in VB

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-11, 09:56
prady1012 prady1012 is offline
Registered User
 
Join Date: May 2011
Posts: 1
Post SQLInsert code in VB

Hello, I am new to writing scripts in VB. My question is..

I have a workbook in Excel and it has suppose columns A,B,C,D,E,F and G. I am generating a sql script from Excel and when I run the script, all the values would be stored in a table in SQL server. I am having trouble in writing the scritp, when I do not want all the columns, but only columns A,C and E. below is a script ive written. Please help..


Public Sub GenerateSQLInserts()
Dim sqlInsert, sqlSelect, sqlOut As String
Dim row, col As Range

sqlInsert = "INSERT INTO [tbl_DMaskLoad]"
sqlOut = ""

For Each row In Range("A1").CurrentRegion.Rows
sqlSelect = "SELECT "
For Each col In row.Cells
sqlSelect = sqlSelect & col & ", "
Next

sqlOut = sqlOut & sqlInsert & vbCrLf & sqlSelect & vbCrLf & vbCrLf
Next

fnum = FreeFile()
Open Application.GetSaveAsFilename(fileFilter:="SQL Files (*.sql), *.sql") For Output As fnum

Print #fnum, sqlOut
Close #fnum

End Sub
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On