假设我有3" SerialNoCubicle"数字作为触发器; " 8"," 17"," 18"。
当combobox.value填充其中一个时,textbox.value的其余部分将从access数据库中替换。问题是,我只能做第一个,也就是说,如果值是" 8"然后替换textboxes.value。但如果" 17"或" 18" textbox.value仍为空。
我可以通过更改
手动完成If txtSerialNoCubicle.Value = 8 or txtSerialNoCubicle.Value = 17 or _
txtSerialNoCubicle.Value = 18 then
但是如果用户在" 8"," 17"," 18"旁边输入另一个值。 for" SerialNoCubicle"然后代码不适用于新值
Private Sub serialnocubicleDB()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim sql1 As String
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ace.OLEDB.12.0; " & _
"Data Source=E:\Database.accdb"
Set rs = New ADODB.Recordset
sql1 = "Select * FROM tblspecification "
rs.Open sql1, cn
If txtSerialNoCubicle.Value = rs.Fields("SerialNoCubicle").Value Then
TextBox1.Value = rs.Fields("Project").Value
TextBox2.Value = rs.Fields("ProjectNo").Value
TextBox3.Value = rs.Fields("No&DateofDrw").Value
TextBox4.Value = rs.Fields("DrawingNumber").Value
TextBox5.Value = rs.Fields("NameofCubicle").Value
TextBox6.Value = rs.Fields("SingleLineLayout").Value
TextBox7.Value = rs.Fields("PlantofTest").Value
TextBox9.Value = rs.Fields("TypeofProduct").Value
TextBox10.Value = rs.Fields("IPofProduct").Value
TextBox11.Value = rs.Fields("Substation").Value
End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
答案 0 :(得分:0)
而不是查询整个表格,只需获取您想要的记录
Private Sub serialnocubicleDB()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim sql1 As String
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ace.OLEDB.12.0; " & _
"Data Source=E:\Database.accdb"
Set rs = New ADODB.Recordset
'Query for the specific serial number
' should be probably using parameters here depending on your use case
sql1 = "Select * FROM tblspecification t where " & _
" t.SerialNoCubicle=" & txtSerialNoCubicle.Value
rs.Open sql1, cn
If Not rs.EOF Then
TextBox1.Value = rs.Fields("Project").Value
TextBox2.Value = rs.Fields("ProjectNo").Value
TextBox3.Value = rs.Fields("No&DateofDrw").Value
TextBox4.Value = rs.Fields("DrawingNumber").Value
TextBox5.Value = rs.Fields("NameofCubicle").Value
TextBox6.Value = rs.Fields("SingleLineLayout").Value
TextBox7.Value = rs.Fields("PlantofTest").Value
TextBox9.Value = rs.Fields("TypeofProduct").Value
TextBox10.Value = rs.Fields("IPofProduct").Value
TextBox11.Value = rs.Fields("Substation").Value
Else
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
TextBox6.Value = ""
TextBox7.Value = ""
TextBox9.Value = ""
TextBox10.Value = ""
TextBox11.Value = ""
End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub