在access数据库中如何将窗体中自动计算的值存入表中
在窗体中有A和B两个文本框,分别对应表中的A和B两个字段。在窗体中B=A*系数,但表中B的值始终为0。请问各位高手如何将窗体中计算B=A*系数的值自动存入表中,回答细点,...
在窗体中有A和B两个文本框,分别对应表中的A和B两个字段。在窗体中B=A*系数,但表中B的值始终为0。请问各位高手如何将窗体中计算B=A*系数的值自动存入表中,回答细点,小弟是新手,在这儿先谢谢各位了!!!!!!!!
展开
1个回答
展开全部
先建个模块,在里面写以下代码
Public Function gosql(ByVal sql As String, msgstring As String) As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
sql = Trim(sql)
Set cn = New ADODB.Connection
cn.Open ConnectionString
If Left(sql, 6) = LCase("insert") Or Left(sql, 6) = LCase("delete") Or Left(sql, 6) = LCase("update") Then
cn.Execute sql
msgstring = Left(sql, 6) & "操作成功"
Else
Set rs = New ADODB.Recordset
rs.Open sql, cn, adOpenKeyset, adLockOptimistic
Set gosql = rs
msgstring = "查询到" & rs.RecordCount & "条记录"
End If
Set rs = Nothing
Set cn = Nothing
End Function
Public Function ConnectionString()
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DATA.mdb;"‘“DATA.MDB”是数据库文件地址
End Function
然后在窗体上建两个文本A,B。和一个按钮输入以下代码
Private Sub Command1_Click()
Dim rs As ADODB.Recordset
Dim msgstring As String
Dim sql As String
sql = "select * from 表 where B='" & Text1.Text & "'"
Set rs = gosql(sql, msgstring)
rs.AddItem
rs("B") = Text1.Text
rs("A") = Text2.Text
rs.Update
End Sub
Private Sub Text2_Change()
On Error Resume Next
Text1.Text = Text2.Text * 2
End Sub
Public Function gosql(ByVal sql As String, msgstring As String) As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
sql = Trim(sql)
Set cn = New ADODB.Connection
cn.Open ConnectionString
If Left(sql, 6) = LCase("insert") Or Left(sql, 6) = LCase("delete") Or Left(sql, 6) = LCase("update") Then
cn.Execute sql
msgstring = Left(sql, 6) & "操作成功"
Else
Set rs = New ADODB.Recordset
rs.Open sql, cn, adOpenKeyset, adLockOptimistic
Set gosql = rs
msgstring = "查询到" & rs.RecordCount & "条记录"
End If
Set rs = Nothing
Set cn = Nothing
End Function
Public Function ConnectionString()
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DATA.mdb;"‘“DATA.MDB”是数据库文件地址
End Function
然后在窗体上建两个文本A,B。和一个按钮输入以下代码
Private Sub Command1_Click()
Dim rs As ADODB.Recordset
Dim msgstring As String
Dim sql As String
sql = "select * from 表 where B='" & Text1.Text & "'"
Set rs = gosql(sql, msgstring)
rs.AddItem
rs("B") = Text1.Text
rs("A") = Text2.Text
rs.Update
End Sub
Private Sub Text2_Change()
On Error Resume Next
Text1.Text = Text2.Text * 2
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询