Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT using vb.Net - how to continue

Status
Not open for further replies.

Fred48

Programmer
Feb 23, 2004
62
US
Hi,

I have a INSERT to an access database that has about twenty fields. Is there anyway to continue the INSERT statement instead of making one continous statement as below:

cmd = New OleDbCommand(" INSERT INTO tblRevResults ([Update Resp Code], [Check Field], [Save Ind], [TF Resp Code], TGSN , TGID, [Carrier Id],[Orig Eqpt Type],[Term Eqpt Type],[Other TGID],[ColorCode],[TGSNSortOrder],[Modified], [OrigSaveInd] ) Values('" & m_FORDownLoadRequest.UpdateRespCode & "', '" & "0" & "','" & m_FORDownLoadRequest.SaveRevInd & "','" & m_FORDownLoadRequest.TFRespCode & "', '" & m_FORDownLoadRequest.TGSN & "', '" & GetTGIDFromRecord() & "', '" & m_FORDownLoadRequest.CarrierID & "', '" & m_FORDownLoadRequest.OrigEqpType & "','" & m_FORDownLoadRequest.TermEqpType & "', '" & m_FORDownLoadRequest.OtherTGID & "', '" & strColor & "','" & intrecordcount & "','" & "N" & '" &m_FORDownLoadRequest.SaveRevInd & "',"')", cnn)

Thanks
 

How about:
Code:
Dim [blue]strIns[/blue] As String = "INSERT INTO tblRevResults ([Update Resp Code], " _
& ""[Check Field], [Save Ind], [TF Resp Code],  TGSN ,  TGID, [Carrier Id], " _
& "[Orig Eqpt Type],[Term Eqpt Type],[Other TGID],[ColorCode],[TGSNSortOrder], " _
& "[Modified], [OrigSaveInd]  ) Values " _
& "('" & m_FORDownLoadRequest.UpdateRespCode & "',  '" & "0" & "', " _
& "'" & m_FORDownLoadRequest.SaveRevInd & "','" & m_FORDownLoadRequest.TFRespCode & "', " _
& "'" & m_FORDownLoadRequest.TGSN & "', '" & GetTGIDFromRecord() & "', " _
& "'" & m_FORDownLoadRequest.CarrierID & "', '" & m_FORDownLoadRequest.OrigEqpType & "', " _
& "'" & m_FORDownLoadRequest.TermEqpType & "', '" & m_FORDownLoadRequest.OtherTGID & "', " _
& "'" & strColor & "','" & intrecordcount & "', " _
& "'" & "N" &  '" &m_FORDownLoadRequest.SaveRevInd & "'[red],"'[/red])"

cmd = New OleDbCommand([blue]strIns[/blue], cnn)



Have fun.

---- Andy
 

After submiting, it does not look right.

The idea was:
Code:
Dim strIns As String = "INSERT INTO tblRevResults " _
    & "(Field1, Field2, Field3, ...) " _
    & " VALUES(123, 'abc', 888, ...)

cmd = New OleDbCommand(strIns, cnn)

Have fun.

---- Andy
 
Here's another example:
Code:
Dim str As String = ""
str = "SomeText" & ControlChars.CrLf
str &= "Some More Text"  & ControlChars.CrLf
str &= "Even More Text"

That will yield the following
Code:
SomeText
Some MoreText
Even More Text
 
Had to change some " and ' around but it works. Below is the code:


strIns = "INSERT INTO tblRevResults ([Update Resp Code], " _
& "[Check Field], [Save Ind], [TF Resp Code], TGSN , TGID, [Carrier Id], " _
& "[Orig Eqpt Type],[Term Eqpt Type],[Other TGID],[ColorCode],[TGSNSortOrder], " _
& "[Modified], [OrigSaveInd] ) Values " _
& "('" & m_FORDownLoadRequest.UpdateRespCode & "', '" & "0" & "', " _
& "'" & m_FORDownLoadRequest.SaveRevInd & "','" & m_FORDownLoadRequest.TFRespCode & "', " _
& "'" & m_FORDownLoadRequest.TGSN & "', '" & GetTGIDFromRecord() & "', " _
& "'" & m_FORDownLoadRequest.CarrierID & "', '" & m_FORDownLoadRequest.OrigEqpType & "', " _
& "'" & m_FORDownLoadRequest.TermEqpType & "', '" & m_FORDownLoadRequest.OtherTGID & "', " _
& "'" & strColor & "','" & intrecordcount & "', " _
& "'" & "N" & "','" & m_FORDownLoadRequest.SaveRevInd & "')"

Thanks for everyones help!!
 

I've always liked [tt]vbNewLine[/tt]:
Code:
Dim str As String = ""
str = "SomeText" & [blue]vbNewLine[/blue]
str += "Some More Text" & [blue]vbNewLine[/blue]
str += "Even More Text"
Easier to remember (for me). Same outcome.

Have fun.

---- Andy
 
Andrzejek said:
I've always liked vbNewLine:

I try to stay away from most of the functions and constants in the Microsoft.VisualBasic namespace. Not that it really matters, it's just that I like to stay consistent when using the .Net libraries if I were to ever switch languages.
 

Fred48, if you stay with your approach, I would:
[tt]
[blue]With m_FORDownLoadRequest[/blue]
strIns = "INSERT INTO tblRevResults ([Update Resp Code], " _
& "[Check Field], [Save Ind], [TF Resp Code], TGSN , TGID, [Carrier Id], " _
& "[Orig Eqpt Type],[Term Eqpt Type],[Other TGID],[ColorCode],[TGSNSortOrder], " _
& "[Modified], [OrigSaveInd]) Values " _
& "('" & .UpdateRespCode & "', '" & "0" & "', " _
& "'" & .SaveRevInd & "','" & .TFRespCode & "', " _
& "'" & .TGSN & "', '" & GetTGIDFromRecord() & "', " _
& "'" & .CarrierID & "', '" & .OrigEqpType & "', " _
& "'" & .TermEqpType & "', '" & .OtherTGID & "', " _
& "'" & strColor & "','" & intrecordcount & "', " _
& "'" & "N" & "','" & .SaveRevInd & "')"
[blue]End With[/blue]
[/tt]

Have fun.

---- Andy
 
Everyone,

Below is the approach I went with because of ease of documentation.

strIns = " INSERT INTO tblRevResults ([Update Resp Code], [Check Field], [Save Ind], [TF Resp Code], TGSN , TGID, [Carrier Id],[Orig Eqpt Type],[Term Eqpt Type],[Other TGID], ColorCode , TGSNSortOrder , Modified , OrigSaveInd ) Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

Using cmd As New OleDbCommand(strIns, cnn)

cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("Update Resp Code", m_FORDownLoadRequest.UpdateRespCode)
cmd.Parameters.AddWithValue("Check Field", "0")
cmd.Parameters.AddWithValue("Save Ind", m_FORDownLoadRequest.SaveRevInd)
cmd.Parameters.AddWithValue("TF Resp Code", m_FORDownLoadRequest.TFRespCode)
cmd.Parameters.AddWithValue("TGSN", m_FORDownLoadRequest.TGSN)
cmd.Parameters.AddWithValue("TGID", GetTGIDFromRecord())
cmd.Parameters.AddWithValue("Carrier Id", m_FORDownLoadRequest.CarrierID)
cmd.Parameters.AddWithValue("Orig Eqpt Type", m_FORDownLoadRequest.OrigEqpType)
cmd.Parameters.AddWithValue("Term Eqpt Type", m_FORDownLoadRequest.TermEqpType)
cmd.Parameters.AddWithValue("Other TGID", m_FORDownLoadRequest.OtherTGID)
cmd.Parameters.AddWithValue("ColorCode", strColor)
cmd.Parameters.AddWithValue("TGSNSortOrder", intrecordcount)
cmd.Parameters.AddWithValue("Modified", "N")
cmd.Parameters.AddWithValue("OrigSaveInd", m_FORDownLoadRequest.SaveRevInd)
cmd.ExecuteNonQuery()

End Using

Again thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top