在access数据库中如何将窗体中自动计算的值存入表中
答案:2 悬赏:80 手机版
解决时间 2021-03-14 19:30
- 提问者网友:浮克旳回音
- 2021-03-14 10:36
在窗体中有A和B两个文本框,分别对应表中的A和B两个字段。在窗体中B=A*系数,但表中B的值始终为0。请问各位高手如何将窗体中计算B=A*系数的值自动存入表中,回答细点,小弟是新手,在这儿先谢谢各位了!!!!!!!!
最佳答案
- 五星知识达人网友:迟山
- 2021-03-14 10:50
先建个模块,在里面写以下代码
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
全部回答
- 1楼网友:迟山
- 2021-03-14 11:58
搜一下:在access数据库中如何将窗体中自动计算的值存入表中
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯