gpt4 book ai didi

mysql - 在输入框中输入错误拼写时返回错误消息

转载 作者:行者123 更新时间:2023-11-30 01:11:00 25 4
gpt4 key购买 nike

我有这段代码,我使用 VBA 在 mySQL 数据库中插入、更新和删除数据。我有一个输入框,可以使用编号在 SQL 表中搜索特定数据,当我键入编号并且存在匹配记录时,程序会显示“找到记录”。但每次我输入数字时程序都会出错并且没有匹配的数据。我的问题是,当没有匹配的数据时,如何返回“未找到记录”的错误消息?我作为初学者正在学习 vba,并且使用的一些代码对我来说并不是很熟悉。谢谢。

  Dim rs As New ADODB.Recordset
myConn
Dim holdstr As String
holdstr = InputBox("Enter name")
If holdstr = "" Then
MsgBox "Please fill up the requirements", vbInformation, "Message"
conn.Close
Exit Sub
End If
rs.Open "SELECT * FROM lemployees where ENumber = '" & holdstr & "'", conn

MsgBox "Record found!", vbInformation, "Message"

UserForm2.TextBox2.Text = rs!ELName

最佳答案

您要做的是在打开 Recordset 后检查 RecordCount 属性。

Dim rcCount As Integer
rs.Open "SELECT * FROM lemployees where ENumber = '" & holdstr & "'", conn
rcCount = rs.RecordCount
If rcRecordCount = 0 Then
MsgBox "No Record found", vbInformation, "Message"
Else
MsgBox "Record found!", vbInformation, "Message"
UserForm2.TextBox2.Text = rs!ELName
End If

关于mysql - 在输入框中输入错误拼写时返回错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com