在模块里写一个函数,来按字段查询, 急!!!在线等待......问题解决立刻给分,谢谢

wjenigma 2003-03-04 11:22:13
我想在一个标准模块中定义一个,查询字段用的函数。在函数中,把查询结果保存在一个字符串中 (最后在窗体中调用这个函数就可以了),请帮我看一下,我的这段代码对不对(这段代码是在模块中定义的),如果不对请帮忙改正,谢谢!


通用部分
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=F:\Access\database.mdb"
cn.Open
Set rs = New Recordset
rs.Open "table", cn, adOpenDynamic, adLockOptimistic, adCmdTable



Public Function find_str() As String
If Text1.Text = Empty & Text2.Text = Empty Then
MsgBox "cannot be empty"
Form1.Text1.SetFocus
Else
find_str = "select * from table1 where col1='" _
& Form1.Text1.Text & "' or col2 = '" & Form1.Text2.Text & "'"
End If
End Function

...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-03-04
  • 打赏
  • 举报
回复

在窗体模块中定义就可以了。也可以这样:
Function find_str(ByVal txt1 As String, ByVal txt2 As String) As String
If txt1 = "" & txt2 = "" Then
MsgBox "cannot be empty"
Else
rs.open "select field_name from table1 where col1='" _
& txt1 & "' or col2 = '" & txt2 & "'", ", cn, adOpenDynamic, adLockOptimistic"
If Not rs.EOF Then
rs.movefirst
find_str = rs(field_name)
End If
End If
End Function
WizardBear 2003-03-04
  • 打赏
  • 举报
回复

Public Function find_str() As String
If Text1.Text = Empty & Text2.Text = Empty Then
MsgBox "cannot be empty"
Form1.Text1.SetFocus
Else
rs.open "select field_name from table1 where col1='" _
& Form1.Text1.Text & "' or col2 = '" & Form1.Text2.Text & "'",", cn, adOpenDynamic, adLockOptimistic
if rs.BOF <> rs.EOF then
rs.movefirst
find_str=rs(field_name)
end if
End If
End Function


或者把上面的
rs.Open "table", cn, adOpenDynamic, adLockOptimistic, adCmdTable
改写为:
rs.Open find_str(), cn, adOpenDynamic, adLockOptimistic

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧