VB.NET - Inserting/Deleting via ODBC Connection - vb.net
I'm connecting to an AS/400 by an ODBC connection. I'm getting wacky errors when I try to insert and delete from the file. First, the format I've found for constructing a query stirng was like this:
"DELETE FROM <library>.<filename> WHERE <field> = <value>"
So, based on that, I wrote this:
"DELETE FROM RM#AVLIB.AV90909JWB WHERE MBR_NUM = " & mbr_num
It threw an error about not liking the DELETE, oddly enough, but I got it to go away if I removed the library altogether:
"DELETE FROM AV90909JWB WHERE MBR_NUM = " & mbr_num
I'm guessing it just didn't like the # symbol and worked without the library because my default library is the same one I'm using here. But now I'm getting an error in my INSERT that says:
ERROR [22003][Micro Focus][RUMBA Data Access][S1023934]Numeric value out of range. SQLCODE = -420
After running through all the values I'm passing, I didn't see any values that were longer than the field length so I'm not sure where this is coming from.
Here's my code as it sits now:
delQry = "DELETE FROM AV90301JWB WHERE MBR_CD = '" & MBR_CD & "' AND LOC_CD = '" & LOC_CD & "' AND PRP_ITM = '" & PRP_ITM & "'"
pushQry = "INSERT INTO AV90301JWB ( " & _
"MBR_NUM, " & _
"LOC_CD, " & _
"AVBLD_CLMT, " & _
"ADDRESS1, " & _
"ADDRESS2, " & _
"CITY, " & _
"STATE, " & _
"ZIPCODE, " & _
"AVBLD_DOS, " & _
"CNST_QLTY, " & _
"SEISMIC, " & _
"WIND, " & _
"AVBLD_DSC, " & _
"AVBLD_DSC1, " & _
"AVBLD_DSC2, " & _
"AVBLD_DSC3, " & _
"MISC_ADJ, " & _
"SEC_ID, " & _
"AVCOS_RC, " & _
"YR_BUILT, " & _
"NBR_STORY, " & _
"SQR_FT, " & _
"SUBCLASS, " & _
"OCC_CD1, " & _
"OCC_DSC1, " & _
"OCC_PCT1, " & _
"STORY_HT1, " & _
"OCC_CD2, " & _
"OCC_DSC2, " & _
"OCC_PCT2, " & _
"STORY_HT2, " & _
"OCC_CD3, " & _
"OCC_DSC3, " & _
"OCC_PCT3, " & _
"STORY_HT3, " & _
"OCC_CD4, " & _
"OCC_DSC4, " & _
"OCC_PCT4, " & _
"STORY_HT4, " & _
"OCC_CD5, " & _
"OCC_DSC5, " & _
"OCC_PCT5, " & _
"STORY_HT5, " & _
"HEAT_SYS, " & _
"COOL_SYS, " & _
"PWALL_EXT, " & _
"ROOF_MAT, " & _
"SPRINKLER, " & _
"MANL_FIRE, " & _
"AUTO_FIRE, " & _
"CNST_PCT1, " & _
"CNST_PCT2, " & _
"CNST_PCT3, " & _
"CNST_PCT4, " & _
"CNST_PCT5) " & _
"VALUES (" & _
"'" & MBR_NUM & "', " & _
"'" & LOC_CD & "', " & _
"'" & AVBLD_CLMT & "', " & _
"'" & ADDRESS1 & "', " & _
"'" & ADDRESS2 & "', " & _
"'" & CITY & "', " & _
"'" & STATE & "', " & _
"'" & ZIPCODE & "', " & _
"'" & AVBLD_DOS.ToShortDateString() & "', " & _
"'" & CNST_QLTY & "', " & _
"'" & SEISMIC & "', " & _
"'" & WIND & "', " & _
"'" & AVBLD_DSC & "', " & _
"'" & AVBLD_DSC1 & "', " & _
"'" & AVBLD_DSC2 & "', " & _
"'" & AVBLD_DSC3 & "', " & _
"'" & MISC_ADJ & "', " & _
"'" & SEC_ID & "', " & _
"" & Math.Round(AVCOS_RC, 2, MidpointRounding.AwayFromZero) & ", " & _
"'" & YR_BUILT & "', " & _
"'" & NBR_STORY & "', " & _
"'" & SQR_FT & "', " & _
"'" & SUBCLASS & "', " & _
"'" & OCC_CD1 & "', " & _
"'" & OCC_DSC1 & "', " & _
"'" & OCC_PCT1 & "', " & _
"'" & STORY_HT1 & "', " & _
"'" & OCC_CD2 & "', " & _
"'" & OCC_DSC2 & "', " & _
"'" & OCC_PCT2 & "', " & _
"'" & STORY_HT2 & "', " & _
"'" & OCC_CD3 & "', " & _
"'" & OCC_DSC3 & "', " & _
"'" & OCC_PCT3 & "', " & _
"'" & STORY_HT3 & "', " & _
"'" & OCC_CD4 & "', " & _
"'" & OCC_DSC4 & "', " & _
"'" & OCC_PCT4 & "', " & _
"'" & STORY_HT4 & "', " & _
"'" & OCC_CD5 & "', " & _
"'" & OCC_DSC5 & "', " & _
"'" & OCC_PCT5 & "', " & _
"'" & STORY_HT5 & "', " & _
"'" & HEAT_SYS & "', " & _
"'" & COOL_SYS & "', " & _
"'" & PWALL_EXT & "', " & _
"'" & ROOF_MAT & "', " & _
"'" & SPRINKLER & "', " & _
"'" & MANL_FIRE & "', " & _
"'" & AUTO_FIRE & "', " & _
"'" & CNST_PCT1 & "', " & _
"'" & CNST_PCT2 & "', " & _
"'" & CNST_PCT3 & "', " & _
"'" & CNST_PCT4 & "', " & _
"'" & CNST_PCT5 & "')"
Dim connectionString As String = ConfigurationManager.AppSettings("iSeriesConnString")
Dim insCommand As New OdbcCommand(pushQry)
Dim delCommand As New OdbcCommand(delQry)
Dim da As New OdbcDataAdapter
Using myConn As New OdbcConnection(connectionString)
insCommand.Connection = myConn
delCommand.Connection = myConn
myConn.Open()
da.InsertCommand = insCommand
da.DeleteCommand = delCommand
da.DeleteCommand.ExecuteNonQuery()
da.InsertCommand.ExecuteNonQuery()
End Using
Does anyone see something I'm missing or have any ideas about how to find my issue?
Thanks!
The standard ANSI SQL way of escaping invalid characters in an identifier or to escape an identifier which conflicts with a reserved keyword is to put it in double quotes:
DELETE FROM "RM#AVLIB".AV90909JWB WHERE ...
Obviously one of the values you are inserting or you are using in the where clause exceeds the range defined for that column. E.g. if a column has been defined as NUMERIC(2), you cannot insert 100.
Also I strongly urge you to use command parameters instead of string concatenation: See https://stackoverflow.com/a/2092851/880990
Related
VBA Insert static value into SQL table
I've used VBA to insert into an SQL table before but its always been using values in cells. I have three columns in my table where I need to add just a static value (so no cell reference). The code I have so far is: stSQL = "INSERT INTO Client_Notes " & _ "(ID, Category, RefID, Name, Notes, Date_Loaded, Latest) " & _ " VALUES (" & _ "'" & 1 & "', " & _ "'" & 1 & "', " & _ "'" & Sheets("Missing").ActiveCell.Offset(0, -10).Value & "', " & _ "'" & Sheets("Missing").ActiveCell.Offset(0, -9).Value & "', " & _ "'" & Sheets("Details").ActiveCell.Value & "', " & _ "'" & Date & "', " & "'" & _ "'" & 1 & "'" & _ ")" The error I keep getting is when trying to insert 1 as a value. I'm sure its straight forward but feel like I had tried every quote and comma combination going. Thanks in advance So the new code now looks like so, where I am getting Subscript Out Of Range: stSQL = "INSERT INTO Client_Notes " & _ "(ID, Category, RefID, Name, Notes, Date_Loaded, Latest) " & _ " VALUES (" & _ "1, " & _ "1, " & _ "'" & Sheets("Missing").Range("L" & i).Offset(0, -10).Value & "', " & _ "'" & Sheets("Missing").Range("L" & i).Offset(0, -9).Value & "', " & _ "'" & Sheets("Details").Range("L" & i).Value & "', " & _ "'" & Date & "', " & _ "1)"
Syntax error in UPDATE statement. via cmd.nonexecutequery
If Me.TextBox1.Tag & "" = "" Then cmd.CommandText = "INSERT INTO Table1(CandidateID, Fname, Mname, Lname, Partylist, Pst, course) " & _ " VALUES (" & Me.TextBox1.Text & ", '" & Me.TextBox2.Text & "', '" & Me.TextBox3.Text & "', '" & _ Me.TextBox4.Text & "', '" & Me.ComboBox1.Text & "', '" & Me.ComboBox2.Text & "', '" & Me.ComboBox3.Text & "')" cmd.ExecuteNonQuery() Else cmd.CommandText = "UPDATE table " & _ " SET CandidateID=" & Me.TextBox1.Text & _ ", Fname='" & Me.TextBox2.Text & "'" & _ ", Mname='" & Me.TextBox3.Text & "'" & _ ", Lname='" & Me.TextBox4.Text & "'" & _ ", Partylist='" & Me.ComboBox1.Text & "'" & _ ", Pst='" & Me.ComboBox2.Text & "'" & _ ", Course='" & Me.ComboBox3.Text & "'" & _ " WHERE CandidateID=" & Me.TextBox1.Tag cmd.ExecuteNonQuery()
cmd.CommandText = "UPDATE Table1 " & _ " SET CandidateID=" & Me.TextBox1.Text & _ ", Fname='" & Me.TextBox2.Text & "'" & _ ", Mname='" & Me.TextBox3.Text & "'" & _ ", Lname='" & Me.TextBox4.Text & "'" & _ ", Partylist='" & Me.ComboBox1.Text & "'" & _ ", Pst='" & Me.ComboBox2.Text & "'" & _ ", Course='" & Me.ComboBox3.Text & "'" & _ " WHERE CandidateID=" & Me.TextBox1.Tag cmd.ExecuteNonQuery() my table was incorrect. thank you for those who replied to my thread
ms access 2007 update error run time error 3464, data type mismatch in criteria expression
i cannot update the data, it says that data type mismatch in criteria experession CurrentDb.Execute "UPDATE PC_Details " & _ " SET Serial_No=" & Me.txtNo & _ ", PC_Brand='" & Me.txtBrand & "'" & _ ", PC_Model='" & Me.txtModel & "'" & _ ", Status='" & Me.txtStatus & "'" & _ ", Description='" & Me.txtDesc & "'" & _ ", Staff_ID='" & Me.txtID & "'" & _ " WHERE Serial_No=" & Me.txtNo.Tag
Do this to debug: Dim SQL As String SQL = "UPDATE PC_Details " & _ " SET Serial_No=" & Me.txtNo & _ ", PC_Brand='" & Me.txtBrand & "'" & _ ", PC_Model='" & Me.txtModel & "'" & _ ", Status='" & Me.txtStatus & "'" & _ ", Description='" & Me.txtDesc & "'" & _ ", Staff_ID='" & Me.txtID & "'" & _ " WHERE Serial_No=" & Me.txtNo.Tag Debug.Print SQL CurrentDb.Execute SQL Then study the resulting SQL and post it here. If all values are present, one or more is text but is handled as number or vice versa.
Syntax error in UPDATE statement vb and ms.access as database
I am trying to make medical record application for my assignment. I found this error to edit data patient. Please help and sorry for my bad grammar Dim aksesedit As String = "Update tbpasien set " & _ "Nama_Pasien='" & txtnamapasien.Text & "', " & _ "Jenis_Kelamin='" & cmbjk.Text & "'," & _ "Tempat_Pasien='" & txttempatlahir.Text & "', " & _ "Tanggal_Lahir='" & tanggallahir.Text & "', " & _ "Alamat='" & txtalamat.Text & "', " & _ "Kelurahan_Desa='" & txtkeldesa.Text & "', " & _ "Kecamatan='" & txtkec.Text & "', " & _ "Kota_Kabupaten='" & txtkotakab.Text & "', " & _ "No_Telepon_HP='" & txtnotelp.Text & "', " & _ "Agama='" & cmbagama.Text & "', " & _ "Kewarganegaraan='" & cmbwarga.Text & "', " & _ "Status_Pernikahan='" & cmbstatus.Text & "', " & _ "Pekerjaan='" & txtpekerjaan.Text & "', " & _ "where No_RM='" & txtnorm.Text & "'" cmd = New OleDbCommand(aksesedit) cmd.Connection = conn cmd.ExecuteNonQuery() syntax error in update was on here cmd.ExecuteNonQuery() For "Tanggal_Lahir" i used datetimepicker, cmb=combobox, txt=textbox Thanks
There should be no comma before the WHERE clause: "Pekerjaan='" & txtpekerjaan.Text & "' " & _ "where No_RM='" & txtnorm.Text & "'"
VB.NET Remainder Function
What I am trying to do is make a line of text appear every 50 strings read. I was trying to find a reaminder function to use on the GlobalVariables.TransferTracker interger, but I couldn't find anything online. Is there such a funciton? Or a differen't/better way to do this? If it helps here is my code: Do While TransferRecord.Read() 'Start of writing to the SQL server. SQLServerConnection.Open() 'SQL statement to transfer all the data that fits the requirements to the SQL server. Dim SQLCommand1 As New SqlCommand("INSERT INTO dbo.b_Pulp_PI_Forte (" & _ "mill, " & _ "keyprinter_datetime, " & _ "bale_line_num, " & _ "pulp_line_id, " & _ "bale_id, " & _ "drop_datetime, " & _ "layboy_position, " & _ "bale_gross_weight, " & _ "gross_value_flag, " & _ "bale_airdry_pct, " & _ "airdry_value_flag, " & _ "sheets_per_bale, " & _ "grader_test_flag, " & _ "dropped_num, " & _ "created_by, " & _ "CreatedDateTime, " & _ "Who_did_it, " & _ "Last_change_datetime) " & _ "VALUES (" & _ "'850', " & _ "'" & ProdDate & "', " & _ "'" & BaleLineNum & "', " & _ "'" & BaleLine & "', " & _ "'" & BaleNumber & "', " & _ "'" & ProdDate & "', " & _ "'0', " & _ "'" & GrossWeight & "', " & _ "'" & GrossWeightFlag & "', " & _ "'" & AirDry & "', " & _ "'" & AirDryFlag & "', " & _ "'0', " & _ "'N', " & _ "'0', " & _ "'BaleTrac', " & _ "'" & Date.Now & "', " & _ "'BaleTrac', " & _ "'" & Date.Now & "')") 'If DisplayCode is checked this will be printed to the screen. If ApplicationPropertiesWindow.DisplayCodechkbx.Checked = True Then MainTextBox.AppendText(Environment.NewLine & SQLCommand1.CommandText) GlobalVariables.DisplayCode = True End If 'Executing the SQL statement. SQLCommand1.Connection = SQLServerConnection SQLCommand1.ExecuteNonQuery() SQLServerConnection.Close() GlobalVariables.TransferTracker = GlobalVariables.TransferTracker + 1 'This is where I would like to have the remainder function. 'Making message to show that program is still running. If GlobalVariables.TransferTracker = 50 Then MainTextBox.AppendText(Environment.NewLine & "50 records transferred.") End If Loop Right now I just have it set up so it will fire at 50 records, because I couldn't find the function.
The remainder is an operator in VB, Mod: If GlobalVariables.TransferTracker Mod 50 = 0 Then … As a general advice, don’t write … = True in your conditions. Its redundancy is redundant.