I wrote code for insert into and update table student_record_database having many foreign keys of other tables.
While running this code it is not accepting text data selected from combo box and giving error invalid number,integrity constraint,and DTpicker also not accepting given date though I have set custom format.
Here's my code below.I am having problem to show text in combo box.
<code>
Private Sub save_Click()
If Val(COMBO4) = 0 Then
SQL = "INSERT INTO STUDENT_RECORD_DATABASE(ROLLNO,FIRST_NAME,MIDDLE_NAME,LAST_NAME,CONTACT,CONTACT1,CONTACT2,ADDRESS,GRADE,DIVID,BLOOD_GROUP,HID,DATE_OF_BIRTH,TRANSPORT,SNAME,MEAL,BUSNO,RUTNO,DID,AID,CARD_TYPE,CARD_NO)"
SQL = SQL + "VALUES(" & Val(COMBO4) & ",'" & Trim(Text2) & "', '" & Trim(Text3) & "', '" & Trim(Text4) & "', " & Val(Text5) & ", " & Val(Text6) & ", " & Val(Text7) & ", '" & Trim(Text8) & "', '" & Trim(Combo1) & "','" & Val(Combo2) & "',"
SQL = SQL + " '" & Trim(Combo3) & "' ,'" & Val(Combo9) & "','" & DTPicker1.Value & "',"
SQL = SQL + " '" & Trim(Combo10) & "' ,'" & Combo5.Text & "' ,'" & Trim(Combo11) & "' ,'" & Combo6.Text & "' ,'" & Val(Combo12) & "' ,'" & Val(Combo7) & "','" & Val(Combo8) & "',"
SQL = SQL + " '" & Trim(Combo13) & "' ," & Text11.Text & " ) "
Set RES = CON.Execute(SQL)
MsgBox ("RECORD INSERTED")
Else
SQL = "UPDATE STUDENT_RECORD_DATABASE SET "
SQL = SQL + "ROLLNO= " & Val(COMBO4) & ","
SQL = SQL + "FIRST_NAME= '" & Trim(Text2) & "',"
SQL = SQL + "MIDDLE_NAME= '" & Trim(Text3) & "',"
SQL = SQL + "LAST_NAME= '" & Trim(Text4) & "',"
SQL = SQL + "CONTACT=" & Val(Text5) & ","
SQL = SQL + "CONTACT1=" & Val(Text6) & ","
SQL = SQL + "CONTACT2=" & Val(Text7) & ","
SQL = SQL + "ADDRESS= '" & Trim(Text8) & "',"
SQL = SQL + "GRADE='" & Trim(Combo1) & "',"
SQL = SQL + "DIVID='" & Val(Combo2) & "',"
SQL = SQL + "BLOOD_GROUP='" & Trim(Combo3) & "',"
SQL = SQL + "HID='" & Val(Combo9) & "',"
SQL = SQL + "DATE_OF_BIRTH=(' " & DateValue(DTPicker1.Value) & " ' ),"
SQL = SQL + "TRANSPORT='" & Trim(Combo10) & "',"
SQL = SQL + "SNAME='" & Trim(Combo5) & "',"
SQL = SQL + "MEAL='" & Trim(Combo11) & "',"
SQL = SQL + "BUSNO='" & Trim(Combo6) & "',"
SQL = SQL + "RUTNO=" & Val(Combo12) & ","
SQL = SQL + "DID='" & Val(Combo7) & "',"
SQL = SQL + "AID='" & Val(Combo8) & "',"
SQL = SQL + "CARD_TYPE='" & Trim(Combo13) & "',"
SQL = SQL + "CARD_NO=" & Val(Text11) & ""
SQL = SQL + "WHERE ROLLNO= " & Val(COMBO4) & ""
MsgBox ("RECORD UPDATED")
End If
End Sub
</code>
Error occurred is invalid month,integrity constraint-parent key not found,invalid number.
It's kinda hard to answer you question with no idea of the structure of STUDENT_RECORD_DATABASE and what you're updating but there are some things you could look at.
First, when inserting dates into Oracle like this you should use the TO_DATE() function on your date columns. I.e. DATE_OF_BIRTH
Next make sure that all foreign table key constraints are satisfied by your update.
There is no need to update ROLLNO if it is your primary key.
Finally make sure that none of the data you are trying to update is bigger that the size of string (E.g. VARCHAR2) columns. Also, some of these must surely be string columns and they are not bounded by quotes. E.g. CONTACT CONTACT1 CONTACT2 and CARD_NO
You need to use the combo box .ItemData property to store your value's ID field:
Do While Not myRecordset.EOF
myCombo.AddItem myRecordset("MyFieldString").Value
myCombo.ItemData(myCombo.NewIndex) = myRecordset("MyFieldID").Value
myRecordset.MoveNext
Loop
To refer to the selected combo value's .ItemData property:
"CARD_TYPE='" & Trim(Combo13.ItemData(Combo13.ListIndex)) & "',"
As for your data formatting issue, not sure about Oracle, but for SQL Server, you have to format the date value as such:
"DATE_OF_BIRTH=(' " & Format$(DTPicker1.Value, "yyyy-mm-dd") & " ' ),"
Related
I have a VBscript that queries the database to pull data out that is based on the shift e.g. graveyard, day, and swing. I need to adjust the time by an hour less only on days that are past 1-6-2019.
The solution I have tried is expanding my if statement and adding an AND function but it won't work because the first if statement is still true.
dim intCoilCount, intTotalSeconds,intSeconds,strDate,strShift
'SQL="select timeStamp, coil_number, entry_gaptime,thickness,width_in,grade from TABLEEEE by timeStamp"
strShift=Request.Form("SHIFT")
strDate=Request.Form("StartDate")
'if date is greater than 2-22-2006 (switchover date) use SCALEFACTOR
'-----start-----------------
if datediff("d",strDate,cdate("2/22/2006")) <= 0 then
SCALEFACTOR=30000.0 / 50.0
else
SCALEFACTOR=1
end if
'-----end-----------------
'Fixed scale factor problem
'-----start-----------------
SCALEFACTOR=1
'-----end-----------------
SQL="select timeStamp, coil_number, entry_gaptime,thickness,width_in,grade from entryCoilData"
if strShift="graveyard" then
SQL = SQL & " where timestamp > '" & cdate(strDate)-1 & " " & "11:00PM" & "'" & _
" and timestamp <= '" & strDate & " " & "7:00AM" & "'"
elseif strShift="graveyard" and strDate >= cdate(1-6-2019) then
SQL = SQL & " where timestamp > '" & strDate & " " & "10:00AM" & "'" & _
" and timestamp <= '" & strDate & " " & "2:00PM" & "'"
elseif strShift="day" then
SQL = SQL & " where timestamp > '" & strDate & " " & "7:00AM" & "'" & _
" and timestamp <= '" & strDate & " " & "3:00PM" & "'"
elseif strShift="day" and strDate >= cdate(1-6-2019) then
SQL = SQL & " where timestamp > '" & strDate & " " & "7:00AM" & "'" & _
" and timestamp <= '" & strDate & " " & "3:00PM" & "'"
else
SQL = SQL & " where timestamp > '" & strDate & " " & "3:00PM" & "'" & _
" and timestamp <= '" & strDate & " " & "11:00PM" & "'"
end if
I would keep that pesky logic out of the SQL string, and do that in vbscript. Something like this (untested):
dim givendate, startdatetime, enddatetime
givendate = cdate(strDate)
startdatetime = CDate(strDate & " " & "3:00PM")
enddatetime = CDate(strDate & " " & "11:00PM")
if strShift="graveyard" then
if givendate >= cdate("1-6-2019") then
startdatetime = CDate(strDate & " " & "10:00AM")
enddatetime = CDate(strDate & " " & "02:00PM")
else
startdatetime = DateADD("d", -1, CDate(strDate & " " & "11:00PM"))
enddatetime = CDate(strDate & " " & "07:00AM")
end if
end if
if strShift="day" then
startdatetime = CDate(strDate & " " & "07:00PM")
enddatetime = CDate(strDate & " " & "03:00PM")
end if
SQL="SELECT timeStamp, coil_number, entry_gaptime,thickness,width_in,grade from entryCoilData"
SQL = SQL & " WHERE timestamp > '" & startdatetime & "'"
SQL = SQL & " AND timestamp <= '" & enddatetime & "'"
response.write(SQL)
This way you just calculate the startdatetime and enddatetime parameters, and execute the same SQL for each case.
Please note that the way you write your SQL statements in ASP leaves you vulnerable to SQL injection attacks.
You might also want to consider writing date strings in ISO format (yyyy-mm-dd), that way the database will always understand the date. When you use cdate("1-6-2019"), this might be june first or january sixth, depending on how your database or OS is configured. When you use cdate("2019-6-1"), this is universally understood as june first.
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
cn = New SqlConnection
cn.ConnectionString = "Data Source=LAPTOP-VLPOS8UG;Initial Catalog=FAMS;Integrated Security=True"
Try
cn.Open()
Dim query As String
query = "INSERT INTO Master (Asset ID, Asset Category, Department, Brand/Make, Product Serial No, Purchase Date, Purchase Order Date, Installation Date, Purchase Cost, Depriciation Rate, Vendor Code, Warranty Details, Processor, RAM, HDD, Operating System, Application/Software, Note, Asset Name, Status)" + "VALUES ('" & Asset_IDTextBox.Text & "','" & Asset_CategoryComboBox.Text & "','" & DepartmentComboBox.Text & "','" & Brand_MakeTextBox.Text & "','" & Product_Serial_NoTextBox.Text & "','" & Purchase_DateDateTimePicker.Text & "','" & Purchase_Order_DateDateTimePicker.Text & "','" & Installation_DateDateTimePicker.Text & "','" & Purchase_CostTextBox.Text & "','" & Depriciation_RateTextBox.Text & "','" & Vendor_CodeComboBox.Text & "','" & Warranty_DetailsComboBox.Text & "','" & ProcessorComboBox.Text & "','" & RAMComboBox.Text & "','" & HDDComboBox.Text & "','" & Operating_SystemComboBox.Text & "','" & Application_SodtwareTextBox.Text & "','" & NoteTextBox.Text & "','" & Asset_NameTextBox.Text & "','" & StatusComboBox.Text & "')"
cmd = New SqlCommand(query, cn)
dr = cmd.ExecuteReader
MessageBox.Show("Data Saved")
cn.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
cn.Dispose()
End Try
End Sub
Since you tagged your question with ssms I suppose your RDBMS is SQL Server.
Your table has spaces in several column names; try using square brackets around column names that contain spaces:
query = "INSERT INTO Master ([Asset ID], [Asset Category],...
I have a form to add user.
I can add, delete rows in the table however I pretend to update if the user already exist.
My goal is press the row in sub form to edit.
but every time I press update it gives me an error.
Run-time error '3075' Syntax error operator in query expression.
the action code I have is this
Private Sub cmdAdd_Click()
'quando se carrega em Adicionar há 2 opcoes
'1-Insert
'2-Update
If Me.txtuserid.Tag & "" = "" Then
'1
CurrentDb.Execute "INSERT INTO user(userid, username, userfunction, usercc) " & _
" VALUES(" & Me.txtuserid & ",'" & Me.txtusername & "','" & Me.txtuserfun & "','" & Me.txtusercc & "')"
Else
'2
CurrentDb.Execute "UPDATE user " & _
" SET userid=" & Me.txtuserid & _
", username=" & Me.txtusername & "'" & _
", userfunction =" & Me.txtuserfun & "'" & _
", usercc =" & Me.txtusercc & "'" & _
" WHERE userid =" & Me.txtuserid.Tag
End If
'clear fields
cmdClear_Click
'refresh
SubForm1.Form.Requery
End Sub
What I'm doing wrong?
Did you miss ' in you update statement;
CurrentDb.Execute "UPDATE user " & _
" SET userid=" & Me.txtuserid & _
", username='" & Me.txtusername & "'" & _
", userfunction ='" & Me.txtuserfun & "'" & _
", usercc ='" & Me.txtusercc & "'" & _
" WHERE userid =" & Me.txtuserid.Tag
Hi I am having a problem storing image to mssql server I tried both Memory stream and BLOB as well and in both scinarios I get the same error "cannot insert the value null into image column msg 515 level 16 state 2 line 1", any suggestion is more than welcome Thanks.
Dim sqlstring = " begin tran;"
sqlstring &= " INSERT INTO tbl_customers (stnname, cardnum,
family_name, city, fam_mem_nu, id_num, mrkz_num) VALUES
('" & stn & "','" & cd & "', '" & fmnm & "','" & ct & "', '"
& fnum & "', '" & idn & "', '" & cntr & "')"
sqlstring &= "INSERT INTO tbl_customers(imag) SELECT * FROM
OPENROWSET(BULK N'c:\temp\tempimg', SINGLE_BLOB) imag ; "
sqlstring &= "commit tran;"
sql.CommandText = sqlstring
sql.Connection = conn
conn.Open()
Dim ms As New MemoryStream
img = CameraControl1.SnapshotSourceImage
img.Save("c:\temp\tempimg", Imaging.ImageFormat.Png)
'CameraControl1.SnapshotSourceImage.Save(ms, Imaging.ImageFormat.Bmp)
' PictureBox3.Image.Save(ms, PictureBox3.Image.RawFormat)
' ms.ToArray()
' Dim data As Byte() = ms.GetBuffer()
' Dim p As New SqlClient.SqlParameter("#img", SqlDbType.VarBinary)
' p.Value = data
' sql.Parameters.Add(p)
Dim x As Integer = sql.ExecuteNonQuery
I have a vb6 project and i need to update a visual foxpro table from a recordset.My issue is when i try to update the table i get error msg:Command contains unregnized phrase/keywords.My problem is situated where the date field is concern.I dont know if i written the last portion of the code right.Here is my code:
rs2.Open "update transac set no_ot_1_5 = " & rs1.Fields("ovt1") & ", no_ot_2_0 = " & rs1.Fields("ovt2") & ", no_ot_3_0" _
& "= " & rs1.Fields("ovt3") & "where code = '" & rs1.Fields("emp_code") & "and transac.date = & trans.txtend &", cn1, adOpenDynamic, adLockPessimistic
Try this:
rs2.Open "update transac set no_ot_1_5 = " & rs1.Fields("ovt1") & ", no_ot_2_0 = " & rs1.Fields("ovt2") & ", no_ot_3_0" _
& "= " & rs1.Fields("ovt3") & " where code = '" & rs1.Fields("emp_code") & "' and transac.date = '" & trans.txtend &"'", cn1, adOpenDynamic, adLockPessimistic
It looks like you were missing a space before the WHERE keyword and you missed a single-quote after emp_code. It also looks like you had a problem with transac date.
If the solution from G Mastros is still not complete, it may be due to incorrect data type of a "Date" field. you may need to change to
transac.date = CTOD('" & trans.txtend &"') "
as if you are sending in a text string, but the date is of a DATE type field, you'll need to have it converted to a VFP recognized function... CTOD() is Convert Character String to a Date.