数据库的问题求助!!

camelmax 2004-08-22 03:34:43
做一个学籍成绩管理软件,以下是登录部分的代码,
程序可以运行,但输入任意用户名,确定后即报错,

Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Public OK As Boolean
Dim miCount As Integer

Private Sub Form_Load()

Dim sBuffer As String
Dim lSize As Long

sBuffer = Space$(255)
lSize = Len(sBuffer)

Call GetUserName(sBuffer, lSize)
If lSize > 0 Then
txtUser.Text = ""
Else
txtUser.Text = vbNullString
End If

OK = False
miCount = 0

End Sub

Private Sub cmdCancel_Click()
OK = False
Me.Hide
End Sub

Private Sub cmdOK_Click()
Dim txtSQL As String
Dim mrc As ADODB.Recordset
Dim MsgText As String

'以下是检查用户名密码

UserName = ""
If Trim(txtUser.Text = "") Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUser.SetFocus
Else
txtSQL = "select * from user_Info where user_ID = '" & txtUser.Text & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)



If mrc.EOF = True Then 《=出错的语句!!!!!!



MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
txtUser.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(txtPassword.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUser.Text)
Else
MsgBox "输入密码不正确,请重新输入!", vbOKOnly + vbExclamation, "警告"
txtPassword.SetFocus
txtPassword.Text = ""
End If
End If
End If

miCount = miCount + 1
If miCount = 3 Then
Me.Hide
End If
Exit Sub
End Sub

运行到 If mrc.EOF = True Then 的时候报错“对象变量或with块变量未设置”,
程序所需数据结构已经生成,并已设置了初始用户名密码,
但问题还是没有解决,望高手指点!!!
...全文
91 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbman2003 2004-08-22
  • 打赏
  • 举报
回复
这个放在模块中就可以了
Public cnn As ADODB.Connection
vbman2003 2004-08-22
  • 打赏
  • 举报
回复
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
不要放在函数中,放在模块中:
Public cnn As ADODB.Connection
Public rst As ADODB.Recordset

camelmax 2004-08-22
  • 打赏
  • 举报
回复
当然引用了啊 -_-||
vbman2003 2004-08-22
  • 打赏
  • 举报
回复
你引用了ADO对象吗?
运行VB,选择“工程\引用”命令,引用“Microsoft AetiveX Date 2.x Library”
camelmax 2004-08-22
  • 打赏
  • 举报
回复
感谢vbman2003(家人)

但是问题依旧啊……
vbman2003 2004-08-22
  • 打赏
  • 举报
回复
可以不用:
Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=studentinfo.dsn;UID=sam;PWD=sam"
End Function

直接:
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String

On Error GoTo ExecuteSQL_Error

sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open "studentinfo","sam","sam"
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function

ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function

camelmax 2004-08-22
  • 打赏
  • 举报
回复
我怀疑是ExecuteSQL(txtSQL, MsgText)
函数的问题
该模块如下:

Option Explicit
Public fMainForm As frmMain
Public UserName As String

Sub Main()
Dim fLogin As New frmLogin
fLogin.Show vbModal
If Not fLogin.OK Then
'Login Failed so exit app
End
End If
Unload fLogin

Set fMainForm = New frmMain
fMainForm.Show
End Sub

Public Function ConnectString() _
As String
'returns a DB ConnectString
ConnectString = "FileDSN=studentinfo.dsn;UID=sam;PWD=sam"
End Function

Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String

On Error GoTo ExecuteSQL_Error

sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function

ExecuteSQL_Error:
MsgString = "查询错误: " & _
Err.Description
Resume ExecuteSQL_Exit
End Function

Public Function Testtxt(txt As String) As Boolean
If Trim(txt) = "" Then
Testtxt = False
Else
Testtxt = True
End If
End Function

请帮帮忙啊
camelmax 2004-08-22
  • 打赏
  • 举报
回复
请教zyg0大哥应该怎样修改呢?
zyg0 2004-08-22
  • 打赏
  • 举报
回复
select * from user_Info where user_ID = '" & txtUser.Text & "'"
语句错误或者是
ExecuteSQL(txtSQL, MsgText)
函数有问题,用我的函数
http://community.csdn.net/Expert/topic/3194/3194714.xml?temp=1.125735E-02

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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