我正在尝试创建一个需要用户登录和注销的财务数据库。我有那个部分正常工作。在数据库的主页上,我试图使用查询显示最后25个(或X个)的事务。出于某种原因,我无法通过代码,因为它显示"数据类型不匹配。"以下是各种代码 - 我将按照以下方式解释每个代码:
全球变量(我的全球模块) 选项比较数据库
'全局变量 全球C长 全球C2一样长 全局HoldString为字符串 全局标志为布尔值 全局回复为字符串 全球mbReply作为VbMsgBoxResult
Global User As String
Global GUser As Long
Global db As Database
以下是登录的Subs()(第一个Sub()用于退出按钮,第二个子()用于登录按钮):
选项比较数据库
Private Sub B_Exit_Click()
mbReply = MsgBox(title:="Exit", _
prompt:="Are you sure you wish to exit the system?", _
Buttons:=vbYesNo)
If mbReply = vbNo Then
Exit Sub
Else
DoCmd.Quit acQuitSaveNone
End If
End Sub
Private Sub B_SignIn_Click() '变量 设置db = CurrentDb() Dim Employees作为DAO.Recordset 设置Employees = db.OpenRecordset(" Employees",dbOpenDynaset)
Dim isEmployeed As Boolean
Dim PassMatch As Boolean
Dim isTerm As Boolean
'检查用户是否在系统中 isEmployeed = False PassMatch = False isTerm = False
Do While Not Employees.EOF
If Employees![UserName] = T_Username.Value Then
isEmployeed = True
'make sure the employee is not terminated
If Employees![Terminated] = "Yes" Then
isTerm = True
End If
If isTerm = True Then
MsgBox ("This user has been terminated.")
Exit Sub
End If
'make sure password is correct
If Employees![Password] = T_Password.Value Then
PassMatch = True
End If
If PassMatch = False Then
MsgBox ("Incorrect Password.")
Exit Sub
End If
'mark signed in
Employees.Edit
Employees![SignedIn] = 1
Employees.Update
User = Employees![FirstName] & " " & Employees![LastName]
GUser = Employees![ID] 'Sets GUswer to equal record ID.
End If
Employees.MoveNext
Loop
If isEmployeed = False Then
MsgBox ("This username is not in the system.")
Exit Sub
End If
'关闭此表单并打开主菜单 Employees.Close DoCmd.OpenForm FormName:=" HomePage" DoCmd.Close acForm,Me.Name,acSaveNo
End Sub
接下来是查询的SQL代码:
SELECT TOP 25 Spend.ID,Spend.Vendor,Spend.MaterialGroup,Spend.GLCode,Spend.CostCenter,Spend.Department,Spend.InvoiceNumber,Spend.InvoiceDate,Spend.Amount,Spend.Tax,Spend.Total, Spend.DateEntered,Spend.DocNumber,Spend.Description,Spend。[付费?],Spend.EnteredBy,Spend.EnteredBy
FROM Spend
WHERE(((花费。[EnteredBy])=" GUser"));
花费。[EnteredBy]与Employees表有关系。因此,由于这种关系,EnteredBy实际上是一个数字字段。
如果我硬编码" WHERE"声明类似于(((花费。[EnteredBy])= 2));然后查询将正常工作。
最终,我想要的是查询显示登录用户完成的最后25个数据条目。
希望这是有道理的。如果有问题,请告诉我。我觉得我错过了一些小事,但我无法理解。
谢谢,
克拉克
答案 0 :(得分:0)
您的查询应为:
SELECT TOP 25 Spend.ID, Spend.Vendor, Spend.MaterialGroup, Spend.GLCode, Spend.CostCenter,
Spend.Department, Spend.InvoiceNumber, Spend.InvoiceDate, Spend.Amount, Spend.Tax, Spend.Total,
Spend.DateEntered, Spend.DocNumber, Spend.Description, Spend.[Paid?], Spend.EnteredBy, Spend.EnteredBy
FROM Spend WHERE (((Spend.[EnteredBy])=" & GUser & "));
请注意我在GUser变量之前和之后放置的&符号(&)。这告诉Access evalute表达式并返回它的VALUE。
我也提醒您不要使用名称" User"作为变量名称。它是Access中的保留字: