1、登陆按钮的代码如下:
Private Sub cmdOK_Click()
Dim mrc As ADODB.Recordset
Dim strmsg As String
txtSQL = "select uid from userinfo where UID='" & Trim(txtUserName.Text) & "'"
Set mrc = ExecuteSQL(txtSQL, strmsg)
If mrc.EOF = True Then
MsgBox " 用户名错误!", vbExclamation + vbOKOnly, "警告"
txtUserName.SetFocus
txtUserName.SelStart = 0
txtUserName.SelLength = Len(txtUserName.Text)
Exit Sub
End If
UserName = mrc.Fields(0)
txtSQL = "select UID from userinfo where PWD='" & Trim(txtPassword.Text) & "'"
Set mrc = ExecuteSQL(txtSQL, strmsg)
If mrc.EOF = True Then 执行后指示此处出错,出错信息如标题所示
MsgBox " 密码错误!", vbExclamation + vbOKOnly, "警告"
txtPassword.SetFocus
txtPassword.SelStart = 0
txtPassword.SelLength = Len(txtPassword.Text)
Exit Sub
End If
OK = True
frmMain.Show
Unload Me
End Sub2、模块代码如下
Public frmMain As frmMain
Public gintMode As Integer
Public flagEdit As Boolean
Public UserName As String
Sub Main()
frmLogin.Show
End Sub
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim ConnectString As String
ConnectString = "Provider=SQLOLEDB.1;" & _
"Persist Security Info=False;" & _
"UID=vb;PWD=sa;" & _
"Initial Catalog=PersonMIS.mdf;" & _
"Data Source=(local)"
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
'将enter换成tab
Public Sub EnterToTab(Keyasc As Integer)
If Keyasc = 13 Then
SendKeys "{TAB}"
End If
End Sub
补充:我已经在sql server 2005中新建了一个vb的用户且密码是sa,并将PersonMIS数据库加入其中,求大神解决,感激涕霖
Private Sub cmdOK_Click()
Dim mrc As ADODB.Recordset
Dim strmsg As String
txtSQL = "select uid from userinfo where UID='" & Trim(txtUserName.Text) & "'"
Set mrc = ExecuteSQL(txtSQL, strmsg)
If mrc.EOF = True Then
MsgBox " 用户名错误!", vbExclamation + vbOKOnly, "警告"
txtUserName.SetFocus
txtUserName.SelStart = 0
txtUserName.SelLength = Len(txtUserName.Text)
Exit Sub
End If
UserName = mrc.Fields(0)
txtSQL = "select UID from userinfo where PWD='" & Trim(txtPassword.Text) & "'"
Set mrc = ExecuteSQL(txtSQL, strmsg)
If mrc.EOF = True Then 执行后指示此处出错,出错信息如标题所示
MsgBox " 密码错误!", vbExclamation + vbOKOnly, "警告"
txtPassword.SetFocus
txtPassword.SelStart = 0
txtPassword.SelLength = Len(txtPassword.Text)
Exit Sub
End If
OK = True
frmMain.Show
Unload Me
End Sub2、模块代码如下
Public frmMain As frmMain
Public gintMode As Integer
Public flagEdit As Boolean
Public UserName As String
Sub Main()
frmLogin.Show
End Sub
Public Function ExecuteSQL(ByVal SQL _
As String, MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim ConnectString As String
ConnectString = "Provider=SQLOLEDB.1;" & _
"Persist Security Info=False;" & _
"UID=vb;PWD=sa;" & _
"Initial Catalog=PersonMIS.mdf;" & _
"Data Source=(local)"
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
'将enter换成tab
Public Sub EnterToTab(Keyasc As Integer)
If Keyasc = 13 Then
SendKeys "{TAB}"
End If
End Sub
补充:我已经在sql server 2005中新建了一个vb的用户且密码是sa,并将PersonMIS数据库加入其中,求大神解决,感激涕霖
解决方案 »
- vb连接ACCESS数据库insert into问题
- 在即时通讯里怎么实现像netmeeting里的共享自己的桌面
- 麻烦啊,救济一下小弟
- 怎样查找zip包里有无某个文件?
- 请问告诉如何自动抓取网页数据,然后读写到access数据库
- 请问在vb中,如何利用sql语句统计access中几个字段中值?
- 使用如下ADO连接时,如果ACCESS数据库有密码的话,密码加在什么地方呢??
- 要在程序最小化时实现一些操作,应该对改写哪个事件?
- 新手求助:怎样将ADO连接数据库的Data Source属性设为可变化的
- vb中复制文件的方法
- VB6.0环境下,代码运行可以,但是生成不了EXE文件
- VB 如何使用Datareport打印MSHFlexGrid中多条选中的记录
执行没有成功,你断点进入executesql里面看看