2016-02-04 57 views
0

請你幫我明白了什麼,毛病下面的代碼:訪問VBA - 數據提供程序或其他服務返回E_FAIL狀態

Private Function FillCalendar() 
Dim cnn As ADODB.Connection 
Dim ssql As String 
Dim rst As ADODB.Recordset 
Set rst = New ADODB.Recordset 
Set cnn = CurrentProject.Connection 
Dim i As Integer 
Dim Records As Integer 
ssql = "SELECT Format(Availability.AvailabilityDate,'mmm dd ddd') AS MyDate FROM Availability WHERE HotelId=3 AND Month(Availability.AvailabilityDate)=Month(Now()) GROUP BY Availability.AvailabilityDate" 
rst.CursorLocation = adUseClient 
rst.Open ssql, cnn 
Records = rst.RecordCount 
For i = 1 To Records 
Me("Text" & i).Visible = True 
Me("Text" & i).Value = rst.Fields!MyDate 
If InStr(Me("Text" & i).Value, "sat") Or InStr(Me("Text" & i).Value, "sun") Then 
Me("Text" & i).BackColor = RGB(179, 45, 0) 
Else 
Me("Text" & i).BackColor = RGB(0, 114, 188) 
End If 
rst.MoveNext 
Next i 
rst.Close 
Set rst = Nothing 
End Function 

在代碼stucks 紀錄= rst.RecordCount,不能找出原因。 我叫可用性表是非常簡單的:

  1. AvailabilityId自動編號長整型(主鍵)
  2. RoomTypeId號(外鍵)
  3. HotelId號(外鍵)
  4. AvailabilityDate日期/時間(格式:ShortDate)
  5. AvailableRooms號(長整型 - 默認值:0)

謝謝很多提前

回答

0

可能是您的查詢運行通過該表,發現沒有匹配的結果。

試試這個,以檢查是否發現任何記錄:

if rst.EOF = false Then 
    Records = rst.RecordCount 
else 
    Debug.Print "No records returned :/" 
End if 
+0

非常感謝,但它仍然顯示錯誤。現在,代碼在if rst.EOF = false處得到。然後 – Kurt

0

末我設法改變一下我的SQL語句,我的代碼來解決這個問題:

Private Function FillDates() 
Dim cnn As ADODB.Connection 
Dim ssql As String 
Dim rst As ADODB.Recordset 
Set cnn = CurrentProject.Connection 
Dim i As Integer 
Dim Records As Integer 
ssql = "SELECT AvailabilityDate FROM Availability WHERE Month(AvailabilityDate)=Month(Now()) GROUP BY AvailabilityDate" 
Set rst = New ADODB.Recordset 
rst.CursorLocation = adUseClient 
rst.Open ssql, cnn 
Records = rst.RecordCount 
For i = 1 To Records 
Me("Text" & i).Value = rst.Fields!AvailabilityDate 
Me("Text" & i).Value = Format(Me("Text" & i), "mmm dd ddd") 
If InStr(Me("Text" & i).Value, "sab") Or InStr(Me("Text" & i).Value, "dom") Then 
Me("Text" & i).BackColor = RGB(179, 45, 0) 
Else 
Me("Text" & i).BackColor = RGB(0, 114, 188) 
End If 
rst.MoveNext 
Next i 
rst.Close 
Set rst = Nothing 
End Function 

感謝你的幫助

相關問題