vb6显示问题
我在mysql5.0中创建了一个数据库。我想显示其中的数据。它有名为登录的表。它有 2 列用户名和密码。在表单中,我有 2 个文本字段用户名和密码,我只想使用数据库值验证输入并显示消息框。从 vb 到数据库的连接已成功建立。但它不验证输入。其给出的错误为“需要对象”。请任何人帮助我,我是VB新手。
我正在使用 vb6 和 mysql5.0 谢谢
代码是:
public con As ADOB.connection
public rs2 As new ADOB.Recordest
public sub preconnection()
set con = New connection
set rs = New recordest
set con = New ADOB.connection
con.connectionString = "DRIVER = {Mysql ODBC 3.51 driver};"_
& "SERVER = localhost;"_
& "DATABASE = vbtest;"_
& "UID = root;"_
& "PWD = ;"
con.cursorLocation =
con.open
end sub
sql = "select *from login"
set rs = con.execute (sql)
if rs.BOF = False Then
While Not rs.EOF
If Ucase(txtlogin.text = trim(rs(0)) Ad txtpassword.text = Trim(rs(1)) Then
username = rs(0)
loginname = True
MsgBox("welcome")
End if
rs.movenext
wend
End Sub
i have created a database in mysql5.0. i want to display the data from it. it has table named login. it has 2 columns username and password. in form i have 2 text fields username and password i just want to validate input with database values and display message box. connection from vb to database is established successfully. but its not validating input. its giving error as 'object required'. please any body help i'm new to vb.
i'm using vb6 and mysql5.0
thank you
code is:
public con As ADOB.connection
public rs2 As new ADOB.Recordest
public sub preconnection()
set con = New connection
set rs = New recordest
set con = New ADOB.connection
con.connectionString = "DRIVER = {Mysql ODBC 3.51 driver};"_
& "SERVER = localhost;"_
& "DATABASE = vbtest;"_
& "UID = root;"_
& "PWD = ;"
con.cursorLocation =
con.open
end sub
sql = "select *from login"
set rs = con.execute (sql)
if rs.BOF = False Then
While Not rs.EOF
If Ucase(txtlogin.text = trim(rs(0)) Ad txtpassword.text = Trim(rs(1)) Then
username = rs(0)
loginname = True
MsgBox("welcome")
End if
rs.movenext
wend
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经声明了一个变量rs2,但您没有在任何地方使用它;相反,您自始至终都引用了一个不存在的变量 rs
You've declared a variable rs2 but you're not using it anywhere; instead, you're referring throughout to a non-existent variable rs
您的代码示例存在一些问题:
VB6 中的延续约定如下所示:
typo:
sql = "select *from login"
->sql =“从登录中选择*”
拼写错误:
If Ucase(txtlogin.text = trim(rs(0)) Ad txtpassword.text
->If Ucase(txtlogin.text = trim(rs(0)) 和 txtpassword.text
There are a few problems with your code sample:
continuation convention in VB6 is like so:
typo:
sql = "select *from login"
->sql = "select * from login"
typo:
If Ucase(txtlogin.text = trim(rs(0)) Ad txtpassword.text
->If Ucase(txtlogin.text = trim(rs(0)) And txtpassword.text
作为提示,如果您设置“Option Explicit”,VB6 应该为您指出一些拼写错误等。如果您没有此设置,但引用了一个不存在的变量(如 rs),它只会在您使用它时为您创建它。
sql = "select *from login"
可能应该是:
sql = "select * from login"
您可能应该声明 sql.
如果 Ucase(txtlogin.text = trim(rs(0)) Ad txtpassword.text = Trim(rs(1)) 那么
应该是
If Ucase(txtlogin.text = trim(rs(0)) 并且 txtpassword.text = Trim( rs(1)) 然后
另外,我认为使用 StrComp 比 UCASE 更有效 - 尽管两者都可以工作。
As a tip, if you set "Option Explicit", VB6 should point out some of the typos, etc for you. If you don't have this set, but refer to a non existent variable (like rs) it will just create it for you as you use it.
sql = "select *from login"
Should probably be:
sql = "select * from login"
You should probably declare sql.
If Ucase(txtlogin.text = trim(rs(0)) Ad txtpassword.text = Trim(rs(1)) Then
should be
If Ucase(txtlogin.text = trim(rs(0)) And txtpassword.text = Trim(rs(1)) Then
Also, I think it's more efficient to use StrComp than UCASE - although both will work.