VB登陆窗体代码。数据库呢是access的 数据库名为data 有一个登陆表 有两个字段名 用户名 密码

2025-04-13 23:08:52
推荐回答(4个)
回答1:

新建vb工程,然后点击“工程”-->“引用”,在以下两个选项前挑勾:

Microsoft Data Environment Instance 1.0

Microsoft ActiveX Objects 2.5 Library

因为你要用Access数据库,所以需要在工程中添加控件。

建立Access数据库,在这个vb工程文件夹新建一个文件夹,命名为table,然后将这个Access数据库文件放入table文件夹中。

源代码:

Private Con_Access As Connections

Private Sub Form_Load()

    Set Con_Access = New Connection

    With Con_Access

        .Provider = "microsoft.jet.oledb.4.0"

        .ConnectionString = App.Path & "\table\ship.mdb"

        .Open

    End With

End Sub

Private Sub cmdOK_Click()

    Dim cmd As New Command

    Dim rd As New Recordset

    cmd.ActiveConnection = Con_Access

    cmd.CommandText = "select username,userpassword from usertable where username='" & Text1.Text & "'"

    On Error GoTo db_error

    Set rd = cmd.Execute

    On Error GoTo 0

    If rd.EOF Then

        MsgBox "用户名不存在。", vbOKOnly + vbInformation, "错误"

        Exit Sub

    End If

db_error:

    MsgBox Err.Description, vbOKOnly + vbInformation, "错误"

End Sub

Private Sub cmdCancel_Click()

    End

End Sub

已经附上截图了,如果还有疑问,发电邮给我rearguard_zero@hotmail.com

回答2:

Private Sub Command1_Click()

 Dim conn As New ADODB.Connection

   Dim rs As New ADODB.Recordset

   Dim Str1 As String

   Dim Str2 As String

   Dim Str3 As String

   Str1 = "Provider=Microsoft.Jet.OLEDB.4.0;"

   Str2 = "Data Source=C:\Documents and Settings\Administrator\桌面\DATA.mdb;"

   Str3 = "Jet OLEDB:Database Password="

   conn.Open Str1 & Str2 & Str3

   strSQL = "select * from 表1 where 编号=1"

   rs.Open strSQL, conn, 3, 3

   If rs!用户名 = Text1.Text And rs!密码 = Text2.Text Then

MsgBox "登陆成功"

Else

MsgBox "密码或用户名错误"

End If

End Sub

回答3:

这是交互操作做的,我的姐,VB不是纯写代码的(当然完全写代码也许也写的出来,那太麻烦了)

新建一个窗体,改名为frmMain
拖两个文体框
拖两个Label 改Caption为“用户名” “密码”,当然应分别放在两文本框的旁边了
往上拖两个按钮,Caption分别改为OK,Cancel
按钮一般放在下部
大概还要拖一个数据访问控件,指定相关的数据库文件
大概还要在单击“OK”按钮的事件中加入代码,判断“用户名”数据库中有没有和“密码”合不合
就这样

回答4:

'zong变量请在代码界面开始的通用部分定义!
Private Sub Command1_Click()
'登录确认
Dim sqldl As String
Dim sf As String
If zong = 3 Then '若用户输入的次数超过三次,则自动退出
Unload Me
Exit Sub
End If
If Text1.Text = "" Then
MsgBox "没有输入用户名,请输入!", vbOKOnly, "警告"
Text1.SetFocus
Text2.Text = ""
zong = zong + 1 '累加输入次数
Exit Sub
End If
Call SJK(db) '调用数据库链接过程
strSQL = "select * from 登录表 where 用户名='" & Text1.Text & "'"
RS.Open strSQL, db, 3, 3
If RS.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly, "警告"
Text1.SetFocus
Text1.Text = ""
Text2.Text = ""
zong = zong + 1
RS.Close
Set RS = Nothing
db.Close
Exit Sub
End If
If RS("密码") <> Text2.Text Then
MsgBox "密码不正确,请重新输入!", vbOKOnly, "警告"
Text2.SetFocus
RS.Close
Set RS = Nothing
db.Close
zong = zong + 1
Exit Sub
End If
RS.Close
Set RS = Nothing
db.Close
Unload 登录窗口
frmMain.Show
End Sub