2015-11-29 21 views
0

我正在執行這兩個查詢。更新查詢中的不同數據類型

Dim ItemID As String 
Dim MemberID As String 
Dim updatLibItemSQL As String 
Dim DueDate As String 

updatLibItemSQL = "UPDATE [tblLibraryItem]" 
updatLibItemSQL = updatLibItemSQL & " SET [Status] = '" & inStockString & "',[CheckedOutToMemberID] = '',[DueDate]=''" 
updatLibItemSQL = updatLibItemSQL & " WHERE [ItemID] = '" & ItemID & "'" 
currDatabase.Execute updatLibItemSQL, dbFailOnError 

Dim checkOutReturnSQL As String 
checkOutReturnSQL = "UPDATE [tblCheckoutReturn]" 
checkOutReturnSQL = checkOutReturnSQL & " SET [ReturnDate] = '" & Date & "'" 
checkOutReturnSQL = checkOutReturnSQL & " WHERE [ItemID] = '" & ItemID & "' AND [MemberID] = '" & MemberID & "' AND [DueDate] = '" & DueDate & "'" 
currDatabase.Execute checkOutReturnSQL, dbFailOnError 

如果從兩個查詢中刪除MemberID和到期日部分,兩者都運行良好。我不知道什麼是錯的。

在表中,MemberID是NUMBER,DueDate是日期/時間類型字段。我不知道如何使它們在第一個查詢中爲空,以及如何在以後的查詢中使用where子句。

請大家幫忙指正,如果我錯了

回答

1

「空」是有點主觀的 - 當你做了初始插件,做什麼值你給了CheckedOutToMemberID和交貨期?數據庫中的一個簡單答案是使用NULL值。 NULL意味着「沒有價值」(實際上它可能意味着任何你想要它的意思,但我們會堅持「沒有價值」)。

在第一個查詢:

updatLibItemSQL = "UPDATE [tblLibraryItem]" 
updatLibItemSQL = updatLibItemSQL & " SET [Status] = '" & inStockString & "',[CheckedOutToMemberID]=NULL,[DueDate]=NULL" 
updatLibItemSQL = updatLibItemSQL & " WHERE [ItemID] = '" & ItemID & "'" 
currDatabase.Execute updatLibItemSQL, dbFailOnError 

在WHERE部分你不能只是說[現場] = NULL,這是因爲NULL不是一個值 - 這是一個狀態字段值在不在。您必須使用IS運算符:[field] IS NULL(或查看它是否有值[field] IS NOT NULL)。在你的情況,如果你想檢查成員Id和交貨期是NULL,則使用:

checkOutReturnSQL = "UPDATE [tblCheckoutReturn]" 
checkOutReturnSQL = checkOutReturnSQL & " SET [ReturnDate] = '" & Date & "'" 
checkOutReturnSQL = checkOutReturnSQL & " WHERE [ItemID] = '" & ItemID & "' AND [MemberID] IS NULL AND [DueDate] IS NULL" 
currDatabase.Execute checkOutReturnSQL, dbFailOnError 

您可以檢查使用IsNull函數字段值是在VBA NULL:https://msdn.microsoft.com/en-us/library/office/gg278616.aspx

+0

因爲我不想檢查NULL第二個查詢,他們必須在該表中 – GitCoder

+0

那麼他們是如何存儲在表MEMBERID空值值? – Rob

1

日期字段從不包含串;它包含空值或日期/時間值。

這樣:

Dim ItemID As String 
Dim MemberID As String 
Dim updatLibItemSQL As String 
Dim DueDate As String 

updatLibItemSQL = "UPDATE [tblLibraryItem]" 
updatLibItemSQL = updatLibItemSQL & " SET [Status] = '" & inStockString & "',[CheckedOutToMemberID] = '',[DueDate] = Null" 
updatLibItemSQL = updatLibItemSQL & " WHERE [ItemID] = '" & ItemID & "'" 
currDatabase.Execute updatLibItemSQL, dbFailOnError 

Dim checkOutReturnSQL As String 
' DueDate must hold a string like "2015/11/28": 
' DueDate = Format(somedatevalue, "yyyy\/mm\/dd") 
checkOutReturnSQL = "UPDATE [tblCheckoutReturn]" 
checkOutReturnSQL = checkOutReturnSQL & " SET [ReturnDate] = #" & Format(Date, "yyyy\/mm\/dd") & "#" 
checkOutReturnSQL = checkOutReturnSQL & " WHERE [ItemID] = '" & ItemID & "' AND [MemberID] = '" & MemberID & "' AND [DueDate] = #" & DueDate & "#" 
currDatabase.Execute checkOutReturnSQL, dbFailOnError