请问VB中如何不通过data控件来直接访问Access数据库的数据?譬如在TextBox中直接显示某字段?
我想尝试着用VB+access编一个保费计算的程序,通过comboBox选择车型,改变comboBox的值时,TextBox显示数据库中所对应的费率值,再设定计算公式来计...
我想尝试着用VB+access编一个保费计算的程序,通过comboBox选择车型,改变comboBox的值时,TextBox显示数据库中所对应的费率值,再设定计算公式来计算得出保费,要求所有的TEXTBOX控件打开程序显示值为第一条数据记录。书上的例子都是TextBox只有绑定data控件才能获得数值。请VB高手教教我,最好能详细点,因为我是一个超级菜鸟,从来都没有编过程序,各位高手不要见笑,呵呵!
展开
2个回答
展开全部
新建一个工程,在窗体上添加您所需要的控件,
点“工程”菜单——》“引用”,选择Micosoft ActiveX Date objects 2.7 library
在Access中建立数据库
这里我假设你的数据库叫“费率”={车型,出保费},只有这一个表。
填写代码如下:
Option Explicit
Private cnnMain As New ADODB.Connection
Private Sub Combo1_Click()
Dim rs As New ADODB.Recordset
If Combo1.Text <> "" Then
rs.Open "select 出保费 from 费率 where 车型='" & Combo1.Text & "'", cnnMain, adOpenKeyset, adLockReadOnly
If rs.RecordCount > 0 Then
Text1.Text = rs!出保费 & ""
Else
Text1.Text = ""
End If
Else
Text1.Text = ""
End If
Set rs = Nothing
End Sub
Private Sub Form_Load()
With Combo1
.Style = 2
End With
Call OpenConnect
call ListType
End Sub
'列出车型
Private Sub ListType()
Dim i As Long
Dim rs As New ADODB.Recordset
With rs
.Open "select distinct 车型 from 费率", cnnMain, adOpenKeyset, adLockReadOnly
Combo1.Clear
For i = 1 To .RecordCount
Combo1.AddItem rs!车型 & ""
.MoveNext
Next i
End With
Set rs = Nothing
End Sub
Private Sub OpenConnect()
With cnnMain
If .State <> adStateClosed Then .Close
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & 您的Access数据库文件路径 & ";Persist Security Info=False"
.Open
End With
End Sub
这样够详细了吧?
点“工程”菜单——》“引用”,选择Micosoft ActiveX Date objects 2.7 library
在Access中建立数据库
这里我假设你的数据库叫“费率”={车型,出保费},只有这一个表。
填写代码如下:
Option Explicit
Private cnnMain As New ADODB.Connection
Private Sub Combo1_Click()
Dim rs As New ADODB.Recordset
If Combo1.Text <> "" Then
rs.Open "select 出保费 from 费率 where 车型='" & Combo1.Text & "'", cnnMain, adOpenKeyset, adLockReadOnly
If rs.RecordCount > 0 Then
Text1.Text = rs!出保费 & ""
Else
Text1.Text = ""
End If
Else
Text1.Text = ""
End If
Set rs = Nothing
End Sub
Private Sub Form_Load()
With Combo1
.Style = 2
End With
Call OpenConnect
call ListType
End Sub
'列出车型
Private Sub ListType()
Dim i As Long
Dim rs As New ADODB.Recordset
With rs
.Open "select distinct 车型 from 费率", cnnMain, adOpenKeyset, adLockReadOnly
Combo1.Clear
For i = 1 To .RecordCount
Combo1.AddItem rs!车型 & ""
.MoveNext
Next i
End With
Set rs = Nothing
End Sub
Private Sub OpenConnect()
With cnnMain
If .State <> adStateClosed Then .Close
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & 您的Access数据库文件路径 & ";Persist Security Info=False"
.Open
End With
End Sub
这样够详细了吧?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询