我有一种情况,必须将“状态”代码从“组合框”保存到表中。此组合框同时显示“状态ID”和“状态描述”。但是在保存时,我只需要保存“状态ID”
以下是完整表单功能的代码。单击“保存”按钮时发生错误。在我将值从组合分配给第77或90行的“ Recordset Column”的行上。
' rs![status_ID] = Me.cboStatus.Column(1)
Option Compare Database
Option Explicit
Dim db As Database
Dim rs,rs2,rs3 As Recordset
Dim SQL,SQL1,SQL2 As String
Dim intChk As Integer
Private Sub btnFirst_Click()
If Not rs.BOF Then
rs.MoveFirst
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnLast_Click()
If Not rs.EOF Then
rs.MoveLast
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnNew_Click()
SQL2 = "select Max(job_ID) as JID from tbl_mst_JobOrder"
Set rs3 = CurrentDb.OpenRecordset(SQL2,dbOpenDynaset,dbSeeChanges)
If Not rs3.EOF And Not rs3.BOF Then
Me.txtJobID = rs3!JID + 1
End If
Set rs3 = Nothing
TxtSetEmpty
End Sub
Private Sub btnNext_Click()
If Not rs.EOF Then
rs.MoveNext
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnPrevious_Click()
If Not rs.BOF Then
rs.MovePrevious
Set_Data
End If
If rs.BOF Then
rs.MoveNext
End If
End Sub
Private Sub btnSave_Click()
Dim SQL As String
IfEmpty
Dim sqlShift As String
If intChk = 1 Then
intChk = 0
Exit Sub
Else
SQL = "select job_ID from qryJobDetails " _
& "where job_ID = " & Me.txtJobID
Set rs2 = CurrentDb.OpenRecordset(SQL)
If Not rs2.EOF Then
Dim CHK As String
Me.lblChk.Caption = rs2![job_ID]
End If
Set rs2 = Nothing
If Me.txtJobID.Value = Me.lblChk.Caption Then
Dim msgUpd,msgNew,strCobSt As String
strCobSt = Me.cboStatus.Column(1)
msgUpd = "Do you want to update Location ID " & Me.lblChk.Caption
If MsgBox(msgUpd,vbYesno,"Location Update") = vbYes Then
rs.Edit
rs![job_Date] = Me.dtpJDate.Value
rs![job_Desc] = Me.txtJobDesc
rs![loc_ID] = Me.txtLocID
rs![status_ID] = Me.cboStatus.Column(1)
rs![Comments] = Me.txtComment
rs.Update
RefreshListBox
End If
Else
msgNew = "Do you want to add New Location"
If MsgBox(msgNew,"Add New Location") = vbYes Then
rs.AddNew
rs![job_ID] = Me.txtJobID
rs![job_Date] = Me.dtpJDate.Value
rs![job_Desc] = Me.txtJobDesc
rs![loc_ID] = Me.txtLocID
rs![status_ID] = Me.cboStatus.Column(1)
rs![Comments] = Me.txtComment
rs.Update
RefreshListBox
End If
End If
End If
End Sub
Private Sub Form_Load()
Set db = CurrentDb
SQL = "Select status_ID,status_Desc from tbl_mst_Status order by status_ID"
Set rs2 = db.OpenRecordset(SQL)
Do Until rs2.EOF
Me.cboStatus.AddItem rs2![status_ID] & "|" & rs2![status_Desc]
rs2.MoveNext
Loop
Set rs2 = Nothing
Set rs = db.OpenRecordset("qryJobDetails",dbSeeChanges)
RefreshListBox
Set_Data
End Sub
Private Sub Set_Data()
If Not rs.BOF And Not rs.EOF Then
Me.txtJobID = rs![job_ID]
Me.dtpJDate = rs![job_Date]
Me.txtJobDesc = rs![job_Desc]
Me.txtLocID = rs![loc_ID]
Me.txtLocDec = rs![location_desc]
Me.cboStatus = rs![status_ID] & "|" & rs![status_Desc]
Me.txtComment = rs![Comments]
End If
End Sub
Private Sub RefreshListBox()
Me.lstJobOrd.RowSource = ""
Me.lstJobOrd.AddItem "Job Order" & ";" & "Job Date" & ";" & "Job Description" & ";" _
& "Loc Description" & ";" & "Loc ID" & ";" & "Sta ID" & ";" _
& "Sta Desc" & ";" & "Comments"
rs.MoveFirst
Do Until rs.EOF
Me.lstJobOrd.AddItem rs![job_ID] & ";" & rs![job_Date] & ";" & rs![job_Desc] & ";" _
& rs![location_desc] & ";" & rs![loc_ID] & ";" & rs![status_ID] & ";" _
& rs![status_Desc] & ";" & rs![Comments]
rs.MoveNext
Loop
rs.MoveFirst
End Sub
Private Sub TxtSetEmpty()
Me.txtJobDesc = ""
Me.dtpJDate = Now()
Me.txtLocDec = ""
Me.cboStatus = ""
Me.txtComment = ""
Me.txtLocID = ""
End Sub
Private Sub lstJobOrd_Click()
With Me.lstJobOrd
Me.txtJobID.Value = .Column(0)
Me.dtpJDate.Value = .Column(1)
Me.txtJobDesc.Value = .Column(2)
Me.txtLocDec.Value = .Column(3)
Me.txtLocID.Value = .Column(4)
Me.cboStatus.Value = .Column(5)
Me.txtComment.Value = .Column(7)
End With
End Sub
Private Sub IfEmpty()
Dim txtCtr As Control
Dim cboCtr As Control
Dim Str As String
Str = Empty
For Each txtCtr In Me.Controls
If TypeOf txtCtr Is TextBox Then
If IsnullOrEmpty(txtCtr) Then
txtCtr.BackColor = RGB(119,192,212)
txtCtr.BorderColor = RGB(157,187,97)
Str = Str & txtCtr.Tag & vbNewLine
Else
txtCtr.BackColor = vbWhite
txtCtr.BorderColor = RGB(192,192)
End If
End If
Next txtCtr
For Each cboCtr In Me.Controls
If TypeOf cboCtr Is ComboBox Then
If IsnullOrEmptyCbo(cboCtr) Then
cboCtr.BackColor = RGB(119,212)
cboCtr.BorderColor = RGB(157,97)
Str = Str & cboCtr.Tag & vbNewLine
Else
cboCtr.BackColor = vbWhite
cboCtr.BorderColor = RGB(192,192)
End If
End If
Next cboCtr
If Isnull(Str) Or Str = "" Then
Exit Sub
Else
MsgBox "Please enter data in the highlited fields. " & vbNewLine & _
String(52,"_") & vbCrLf & Str,vbInformation + vbOKOnly,"Data not Complete"
intChk = 1
Exit Sub
End If
End Sub
Private Sub txtLocDec_KeyDown(KeyCode As Integer,Shift As Integer)
If KeyCode = 113 Then
DoCmd.OpenForm "frmLocSer",acNormal,acFormAdd,acWindowNormal
End If
End Sub`