2个回答
展开全部
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim loginflag As Boolean
Private Sub Command1_Click()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'建立无源数据库连接
conn.ConnectionString = "driver={sql server};server=12-3FDA18CE0DDC;uid=sa;pwd=sa;Database=test"
conn.ConnectionTimeout = 50
conn.Open
Dim str As String
'连接连接对象
Set rs.ActiveConnection = conn
'设置游标类型
rs.CursorType = adOpenDynamic
If Trim(Text1.Text = "") Then
MsgBox "用户名不能为空,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
'主要看上边这几句
Text1.SetFocus
Else
'设置查询字符串
str = "select * from tt where name ='" & Trim(Text1.Text) & "'"
rs.Open str
If rs.EOF = True Then
MsgBox "这个用户已经存在,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Else
'登录成功,连接主窗口
If (Trim(Text2.Text) = Trim(rs.Fields("password"))) Then
loginflag = True
Me.Hide
Form2.Show
MsgBox "登陆成功"
Else
MsgBox "密码不正确,请重输入密码!", , "警告"
Text2.SetFocus
SendKeys "{Home}+{End}"
End If
End If
End If
End Sub
Private Sub Command2_Click()
Me.Hide
loginflag = False
End
End Sub
Dim rs As ADODB.Recordset
Dim loginflag As Boolean
Private Sub Command1_Click()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'建立无源数据库连接
conn.ConnectionString = "driver={sql server};server=12-3FDA18CE0DDC;uid=sa;pwd=sa;Database=test"
conn.ConnectionTimeout = 50
conn.Open
Dim str As String
'连接连接对象
Set rs.ActiveConnection = conn
'设置游标类型
rs.CursorType = adOpenDynamic
If Trim(Text1.Text = "") Then
MsgBox "用户名不能为空,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
'主要看上边这几句
Text1.SetFocus
Else
'设置查询字符串
str = "select * from tt where name ='" & Trim(Text1.Text) & "'"
rs.Open str
If rs.EOF = True Then
MsgBox "这个用户已经存在,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Else
'登录成功,连接主窗口
If (Trim(Text2.Text) = Trim(rs.Fields("password"))) Then
loginflag = True
Me.Hide
Form2.Show
MsgBox "登陆成功"
Else
MsgBox "密码不正确,请重输入密码!", , "警告"
Text2.SetFocus
SendKeys "{Home}+{End}"
End If
End If
End If
End Sub
Private Sub Command2_Click()
Me.Hide
loginflag = False
End
End Sub
展开全部
写函数调用
Public Function Find(Grid As String,col as Strint, str1 As String, Optional str2 As String="", Optional str3 As String="") As String
Dim rs As New ADODB.Recordset
Dim sql As String
Dim Tmg As String
Set rs = New ADODB.Recordset
sql = "select " & Tcol & " from " & Grid & " where " & str1
If str2<>"" Then sql=sql & " and " & str2
If str3<>"" Then sql=sql & " and " & str3
rs.Open sql, cn, adOpenKeyset, adLockPessimistic
If rs.RecordCount = 0 Then
Tmg = ""
Else
Tmg = rs.Fields(0)
End If
Find = Tmg
rs.Close
Set rs = Nothing
End Function
调用
sub command_click()
if find("查寻的表","查寻的列","要比较的列=text1.text")="" then
megbox "没有"
end if
end sub
Public Function Find(Grid As String,col as Strint, str1 As String, Optional str2 As String="", Optional str3 As String="") As String
Dim rs As New ADODB.Recordset
Dim sql As String
Dim Tmg As String
Set rs = New ADODB.Recordset
sql = "select " & Tcol & " from " & Grid & " where " & str1
If str2<>"" Then sql=sql & " and " & str2
If str3<>"" Then sql=sql & " and " & str3
rs.Open sql, cn, adOpenKeyset, adLockPessimistic
If rs.RecordCount = 0 Then
Tmg = ""
Else
Tmg = rs.Fields(0)
End If
Find = Tmg
rs.Close
Set rs = Nothing
End Function
调用
sub command_click()
if find("查寻的表","查寻的列","要比较的列=text1.text")="" then
megbox "没有"
end if
end sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询