2014-07-10 45 views
0

我正在嘗試創建一個需要用戶登錄和註銷的財務數據庫。我有那部分工作正常。在數據庫的主頁上,我試圖使用查詢來顯示他們最後的25個(或X個)事務。出於某種原因,我無法讓代碼通過,因爲它顯示「數據類型不匹配」。這裏是各種代碼 - 我會解釋每個我去:訪問登錄模塊

全局變量(我的全球模塊) 選項比較數據庫

「的全局變量 全局C只要 全球C2只要 全球HoldString作爲字符串 全局標誌爲布爾 全球回覆作爲字符串 全球mbReply作爲VbMsgBoxResult

Global User As String 
Global GUser As Long 

Global db As Database 

以下是替補()記錄我N(第一子()是用於退出按鈕,第二子()是用於在按鈕符號):

選項比較數據庫

私人小組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 

結束子

私人小組B_SignIn_Click() 「變量設置 DB = CurrentDb() 昏暗公司員工DAO.Recordset 組僱員= db.OpenRecordset( 「僱員」,dbOpenDynaset)

Dim isEmployeed As Boolean 
Dim PassMatch As Boolean 
Dim isTerm As Boolean 

'請檢查用戶系統中的 isEmployeed =假 PassMatch =假 isTerm =假

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窗體名稱:= 「主頁」 DoCmd.Close acForm,Me.Name,acSaveNo

結束子

下一個是查詢我的SQL代碼:

SELECT TOP 25 Spend.ID,Spend.Vendor,Spend.MaterialGroup,Spend.GLCode,Spend.CostCenter,Spend.Department,Spend.InvoiceNumber,花.InvoiceDate,Spend.Amount,Spend.Tax,Spend.Total,Spend.DateEntered,Spend.DocNumber,Spend.Description,花。[付費?],Spend.EnteredBy,Spend.EnteredBy

FROM花

WHERE(((Spend。[EnteredBy])=「GUser」));

支出[EnteredBy]與Employees表有關係。所以EnteredBy實際上是一個數字字段,因爲這種關係。 ((((Spend。[EnteredBy])= 2));如果我將「WHERE」語句硬編碼爲(((Spend。[EnteredBy])= 2));那麼查詢將正常工作。

最終,我想要發生的是查詢顯示登錄用戶完成的最後25個數據條目。

希望這是有道理的。如果有問題,請告訴我。我覺得我錯過了一些小事,但我無法弄清楚。

感謝,

克拉克

回答

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評估該表達式並返回它的VALUE值。

我也提醒你不要使用名稱「User」作爲變量名稱。這是一個保留字在Access:

http://office.microsoft.com/en-us/access-help/access-2007-reserved-words-and-symbols-HA010030643.aspx

+0

我想這樣做,它仍然會返回一個數據不匹配。與「GUser」的值沒有傳遞給查詢有什麼關係?如果我硬編碼一個數字,例如:Spend。[EnteredBy] = 3,那麼它工作正常。出於某種原因,「GUser」與數字沒有關聯似乎存在問題。 – user3826462