选择时搜索功能运行时错误 424

发布于 2024-11-01 22:39:15 字数 1048 浏览 0 评论 0原文

我的要求:使用VB表单和3个文本框在数据库中搜索数据。

1 文本框,我将输入(用户名)
1 个位置文本框
1 用于显示输出的文本框

我的代码是

Private Sub CommandButton3_Click()

Dim Cn As ADODB.Connection '* Connection String

Dim oCm As ADODB.Command '* Command Object

Dim sName As String
Dim rs As ADODB.Recordset
Dim uname As String
Set Cn = New ADODB.Connection

Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\test2.accdb;Persist Security Info=False"
Cn.ConnectionTimeout = 40
Cn.Open
Set oCm = New ADODB.Command
oCm.ActiveConnection = Cn

‘Record Set
Set rs = New ADODB.Recordset

‘Select Operation
rs.Open "SampleTable", Cn, adOpenKeyset, adLockPessimistic, adCmdTable

uname = rs("UserName")

rs.Open "Select * from SampleTable where uname = '" & text1.Text & "'", ActiveConnection, adOpenForwardOnly, adLockReadOnly, adCmdText

**'Display the Output in TextBox3**
TextBox3.Text = rs("UserName") + rs("Location")

rs.Close
Cn.Close
End Sub

我可以理解数据填充没有发生,因此在 Select 语句中出现运行时错误 424。如何检索给定相应输入的数据?

My requirement: To search for data in database, using VB forms and 3 text boxes.

1 TextBox, I will give Input (UserName)
1 TextBox for Location
1 TextBox for displaying output

My code is

Private Sub CommandButton3_Click()

Dim Cn As ADODB.Connection '* Connection String

Dim oCm As ADODB.Command '* Command Object

Dim sName As String
Dim rs As ADODB.Recordset
Dim uname As String
Set Cn = New ADODB.Connection

Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\test2.accdb;Persist Security Info=False"
Cn.ConnectionTimeout = 40
Cn.Open
Set oCm = New ADODB.Command
oCm.ActiveConnection = Cn

‘Record Set
Set rs = New ADODB.Recordset

‘Select Operation
rs.Open "SampleTable", Cn, adOpenKeyset, adLockPessimistic, adCmdTable

uname = rs("UserName")

rs.Open "Select * from SampleTable where uname = '" & text1.Text & "'", ActiveConnection, adOpenForwardOnly, adLockReadOnly, adCmdText

**'Display the Output in TextBox3**
TextBox3.Text = rs("UserName") + rs("Location")

rs.Close
Cn.Close
End Sub

I can understand the population of data is not happening so getting a RunTime error 424 at Select statement. How can I retrieve the data for the corresponding Input given?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2024-11-08 22:39:15
  1. 您无需打开桌子; rs.在搜索之前打开“SampleTable”
  2. 不确定这是做什么用的; uname = rs("UserName")
  3. 你应该运行; rs.Open "Select * ... (在同一个 RS 上运行 .Open 两次而不关闭第一个可能是导致错误的原因)
  4. 您应该转义您的输入;replace$(text1 .Text, "'", "''")
  5. 之后检查 rs.EOF
  6. 您应该在 .open使用 & > 不是用于连接字符串

更新的 +

sub xxx
Dim Cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sName As String

Set Cn = New ADODB.Connection
Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\test2.accdb;Persist Security Info=False"
Cn.ConnectionTimeout = 40
Cn.Open

Set rs = New ADODB.Recordset

sName = replace$(text1.Text, "'", "''")
rs.Open "Select * from SampleTable where UserName = '" & sName & "'", Cn, adOpenForwardOnly, adLockReadOnly, adCmdText

if (rs.eof) then
    msgbox "no match"
else
    TextBox3.Text = rs("UserName") & " " & rs("Location")
    rs.Close
end if

set rs = nothing
Cn.Close
set cn = nothing
end sub
  1. You do not need to open the table; rs.Open "SampleTable" before searching it.
  2. Not sure what this is for; uname = rs("UserName")
  3. You should just run; rs.Open "Select * ... (Running the .Open on the same RS twice without closing the first is probably whats causing your error)
  4. You should escape your input; replace$(text1.Text, "'", "''")
  5. You should check for rs.EOF after the .open
  6. Use & not + for concatenating strings

update

sub xxx
Dim Cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sName As String

Set Cn = New ADODB.Connection
Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\test2.accdb;Persist Security Info=False"
Cn.ConnectionTimeout = 40
Cn.Open

Set rs = New ADODB.Recordset

sName = replace$(text1.Text, "'", "''")
rs.Open "Select * from SampleTable where UserName = '" & sName & "'", Cn, adOpenForwardOnly, adLockReadOnly, adCmdText

if (rs.eof) then
    msgbox "no match"
else
    TextBox3.Text = rs("UserName") & " " & rs("Location")
    rs.Close
end if

set rs = nothing
Cn.Close
set cn = nothing
end sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文