机房收费系统已经做了很长一段时间了,虽然到目前为止,仍然没有结束,但已经结节尾声了。我感觉现在有必要回首总结一下整个机房收费系统。
除了结账做了一半,报表接触一点之外,其他的都基本上差不多了。从做过的这些和正要做的来分析机房收费,我把他分成了几个部分:查询数据库类(简单查询显示,组合查询)、向数据库写入数据类、导出表格类、报表类、各个表格之间相互连接类(结账)。
以登陆窗体为例子,简单总结一下VB查询数据库实现登陆系统。我的登陆窗体设计如下:
设计登陆窗体后,设计登陆模块,代码如下:
Public UserName As String
Sub Main()
Dim fLogin As New frmlogin
fLogin.Show vbModal '显示登录窗体实例
'OK为fMainForm类的成员
If Not fLogin.OK Then '条件选的好
'Login Failed so exit app
End
End If
Unload fLogin
Set fMainForm = New frmMain '显示主窗体实例
fMainForm.Show
End Sub
'以文件DSN标记,访问ODBC数据源
Public Function ConnectString() As String
ConnectString = "Provider=sqlOLEDB.1;Persist Security Info=False;User ID=**;PassWord=******;Initial Catalog='数据库';Data Source=使用者IP"
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 '非Select语句
cnn.Execute sql '数据量不大时,可以在连接上,直接执行sql语句
MsgString = sTokens(0) & " query successful"
'虽然MsgString不是返回值,但传递方式是ByRef,实参地址和这个地址相同
Else 'Select语句
Set rst = New ADODB.Recordset
rst.Open Trim$(sql),cnn,adOpenKeyset,adLockOptimistic
'得到临时表,游标指向第一条记录
'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
通过模块,连接到数据库中,然后再设置登陆窗体的代码,在登陆窗体中,点击登陆,先判断用户名和密码是否符合要求,然后进入数据库用户表中,查询用户是否存在,若用户存在,查询密码是否正确,若都正确,则显示主窗体,登陆窗体隐藏。
我的代码如下,仅供参考(欢迎提错)
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 Count As Integer
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(txtUserName.Text = "") Then '查询用户名是否存在
MsgBox "没有这个用户,请重新输入用户名!",vbOKOnly + vbExclamation,"警告"
txtUserName.SetFocus
Else
txtsql = "select * from user_Info where userID ='" & txtUserName.Text & "'"
Set mrc = Executesql(txtsql,MsgText)
If mrc.EOF Then
MsgBox "没有这个用户,请重新输入用户名!","警告"
txtUserName.SetFocus
Else
If Trim(mrc.Fields(1)) = Trim(UserID.Text) Then
OK = True
mrc.Close
Me.Hide
UserName = Trim(txtUserName.Text)
Else
MsgBox "输入密码不正确,请重新输入!","警告"
UserID.SetFocus
UserID.Text = ""
End If
End If
End If
Count = miCount + 1'限制登陆次数
If Count = 3 Then
Me.Hide
End If
Exit Sub
End Sub
Private Sub Form_Load()
Dim sBuffer As String
Dim lSize As Long
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer,lSize)
'API中字符串做参数,需要提前确定大小
If lSize > 0 Then
txtUserName.Text = ""
Else
txtUserName.Text = vbNullString '空字符串
End If
OK = False
miCount = 0
End Sub