VB6程序查询数据库

VB6程序查询数据库,第1张

既然是多方式模糊查询,那么必须具备几个条件

1、首先有查询方式的选择框,还有查询需要的关键字的输入框;

2、模糊查询结果不是一条记录,那么必须将查询出的所有结果在列表框控件中显示出来;

3、点击列表框中的一条记录,在文本框中显示这条记录的详细内容。

我比较喜欢用MSFlexGrid控件显示查询结果,下面给你一个比较实用的范例:

Private Sub Command1_Click() '查询

If Combo1Text = "" Then

MsgBox "你没有选择查询方式,请选择!", 16, "错误!"

Exit Sub

End If

If Text1Text = "" Then

MsgBox "你没有填写关键字,请填写!", 16, "错误!"

Exit Sub

End If

If Combo1Text = "查询条件1" Then

strSQL = "Select From 数据表名 字段名1 Like '%" & Text1Text & "%'"

ElseIf Combo1Text = "查询条件2" Then

strSQL = "Select From 数据表名 Where 字段名2 Like '%" & Text1Text & "%'"

ElseIf Combo1Text = "查询条件3" Then

strSQL = "Select From 数据表名 Where 字段名3 Like '%" & Text1Text & "%'"

End If

Call SJK(db) '数据库连接函数

RSOpen strSQL, db, 2, 2

Do While Not RSEOF

SST = SST + 1

RSMoveNext

Loop

RSClose

Set RS = Nothing

If SST = 0 Then

MsgBox "你没有你查询的记录!", 16, "无记录!"

With MSFlexGrid1

Cols = 3

Rows = 1

TextMatrix(0, 0) = " 序号"

TextMatrix(0, 1) = " 姓名"

ColWidth(0) = 1000

ColWidth(1) = 1000

ColWidth(2) = 0

End With

dbClose

Set db = Nothing

Exit Sub

End If

With MSFlexGrid1

Cols = 3

Rows = SST + 1

TextMatrix(0, 0) = " 序号"

TextMatrix(0, 1) = " 姓名"

ColWidth(0) = 1000

ColWidth(1) = 1000

ColWidth(2) = 0

RSOpen strSQL, db, 2, 2

For I = 1 To SST

TextMatrix(I, 0) = I

TextMatrix(I, 1) = RS!姓名

If Option1(0)Value = True Then

TextMatrix(I, 2) = RS!SID

ElseIf Option1(1)Value = True Then

TextMatrix(I, 2) = RS!SID

ElseIf Option1(2)Value = True Then

TextMatrix(I, 2) = RS!SID

ElseIf Option1(3)Value = True Then

TextMatrix(I, 2) = RS!ZID

ElseIf Option1(4)Value = True Then

TextMatrix(I, 2) = RS!SID

End If

RSMoveNext

Next I

RSClose

Set RS = Nothing

End With

dbClose

Set db = Nothing

Label1(2)Caption = "本次查询结果" & SST & "条"

End Sub

Private Sub MSFlexGrid1_Click()

If Not Val(Trim(MSFlexGrid1TextMatrix(MSFlexGrid1Row, 2))) = 0 Then

PKID = Val(Trim(MSFlexGrid1TextMatrix(MSFlexGrid1Row, 2)))

Call SJK(db)

strSQL = "select from 数据表名 Where ID=" & PKID & " Order By ID"

RSOpen strSQL, db, 3, 3

Text2(0)Text = RS!字段1

Text2(1)Text = RS!字段2

Text2(2)Text = RS!字段3

Text2(3)Text = RS!字段4

Text2(4)Text = RS!字段5

Text2(5)Text = RS!字段6

Text2(6)Text = RS!字段7

Text2(7)Text = RS!字段8

RSClose

Set RS = Nothing

dbClose

Set db = Nothing

End If

End Sub

Private Sub Command1_Click()

'建立链接

Dim Cntion As ADODBConnection

Dim RecSet As ADODBRecordset

Dim ConStr, RecStr As String

Set Cntion = New ADODBConnection

Set RecSet = New ADODBRecordset

ConStr = "Provider=MicrosoftJetOLEDB40;Persist Security Info=False;Data Source=D:\1mdb"

CntionOpen ConStr

'模糊搜索语句

RecStr = "SELECT FROM 表1 WHERE TEXT1 LIKE '%" & Text1Text & "%'"

'打开数据库

RecSetOpen RecStr, Cntion, adOpenKeyset, adLockOptimistic

'如果查询结果为空,跳到NotExist

If RecSetBOF <> True And RecSetEOF <> True Then

RecSetMoveFirst

Else

RecSetClose

GoTo NotExist

End If

'输出查询结果。其中输出文本框为MultiLine,可以输出多个结果。

Text2Text = ""

Do While RecSetEOF = False

Text2Text = Text2Text & RecSetFields("TEXT1") & Chr(13) & Chr(10)

RecSetMoveNext

Loop

RecSetClose

Exit Sub

NotExist:

'输出第一行

RecStr = "SELECT FROM 表1"

RecSetOpen RecStr, Cntion, adOpenKeyset, adLockOptimistic

If RecSetBOF <> True And RecSetEOF <> True Then

RecSetMoveFirst

End If

Text2Text = ""

Text2Text = RecSetFields("TEXT1")

RecSetClose

End Sub

Private Sub Command1_Click()

Dim Conn As New ADODBConnection

Dim Rs As New ADODBRecordset

ConnOpen connstring ="Provider=MicrosoftJetOLEDB40;Data Source=" & “数据库的路径和名字” & ";Persist Security Info=True;Jet OLEDB:Database Password= & "数据库密码"

sql = "Select From “ & ”表名" & where ID=20"

RsOpen sql, Conn, 1, 3

If NOT rseof then

m_date= rs("日期型字段的名字")

m_data= rs("数值型字段的名字")

end if

end sub

select from 表 where

(case when 条件 then 1 else 0 end+

case when 条件 then 1 else 0 end+

case when 条件 then 1 else 0 end+

case when 条件 then 1 else 0 end+

case when 条件 then 1 else 0 end) BETWEEN 2 and 5

以上就是关于VB6程序查询数据库全部的内容,包括:VB6程序查询数据库、急!VB怎样实现数据库的查找功能、vb中access数据库查询等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10148587.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存