0

我编写了插入和更新表 student_record_database 的代码,该表有许多其他表的外键。

运行此代码时,它不接受从组合框中选择的文本数据,并给出错误无效数字、完整性约束和 DTpicker 也不接受给定日期,尽管我设置了自定义格式。

下面是我的代码。我在组合框中显示文本时遇到问题。

<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. 
4

2 回答 2

1

在不了解 STUDENT_RECORD_DATABASE 的结构以及您正在更新的内容的情况下,很难回答您的问题,但是您可以查看一些内容。

首先,当像这样将日期插入 Oracle 时,您应该在日期列上使用 TO_DATE() 函数。IEDATE_OF_BIRTH

接下来确保您的更新满足所有外表键约束。

ROLLNO如果它是您的主键,则无需更新。

最后确保您尝试更新的数据都没有大于字符串(例如VARCHAR2)列的大小。此外,其中一些肯定是字符串列,并且它们不受引号限制。例如CONTACT CONTACT1 CONTACT2CARD_NO

于 2013-01-02T09:07:22.513 回答
0

您需要使用组合框.ItemData属性来存储您的值的 ID 字段:

Do While Not myRecordset.EOF
   myCombo.AddItem myRecordset("MyFieldString").Value
   myCombo.ItemData(myCombo.NewIndex) = myRecordset("MyFieldID").Value
   myRecordset.MoveNext
Loop

要引用所选组合值的.ItemData属性:

"CARD_TYPE='" & Trim(Combo13.ItemData(Combo13.ListIndex)) & "',"

至于您的数据格式问题,不确定 Oracle,但对于 SQL Server,您必须将日期值格式化为:

"DATE_OF_BIRTH=(' " & Format$(DTPicker1.Value, "yyyy-mm-dd") & " ' ),"
于 2013-01-02T17:31:07.173 回答