Leo_Polla_Psemata
Registered User.
- Local time
- Yesterday, 21:41
- Joined
- Mar 24, 2014
- Messages
- 364
Hi
I get this error when i try to play with this code
Error number :91 = Object variable or With variable not set
The purpose, one very simple table consisted of four fields, i just want to get these fields exported in an excel sheet.
I get this error when i try to play with this code
Error number :91 = Object variable or With variable not set
The purpose, one very simple table consisted of four fields, i just want to get these fields exported in an excel sheet.
Code:
Private Sub Btn1_Click()
On Error GoTo SubError
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim SQL As String
Dim rsBS As DAO.Recordset
Dim i As Integer
'Show user work is being performed
DoCmd.Hourglass (True)
'*********************************************
' RETRIEVE DATA
'*********************************************
'SQL statement to retrieve data from database
SQL = "SELECT Forma1.IDM, Forma1.bk, Forma1.freight, Forma1.curre " & _
"FROM Forma1;"
'*********************************************
' BUILD SPREADSHEET
'*********************************************
'Create an instance of Excel and start building a spreadsheet
'Early Binding
Set xlApp = Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
Range("A4").Select
ActiveWindow.FreezePanes = True
' Here I try to remove grid
xlSheet.Activate
ActiveWindow.DisplayGridlines = False
With xlSheet
.Name = "IMPORT BLss"
.Cells.Font.Name = "Calibri"
.Cells.Font.Size = 10
'Format Labels
.Range("A1").Value = "ID"
.Range("B1").Value = "BK"
.Range("C1").Value = "FREIGHT"
.Range("D1").Value = "CURRENCY"
'provide initial value to row counter
i = 2
'Loop through recordset and copy data from recordset to sheet
Do While Not rsBS.EOF
.Range("A" & i).Value = Nz(rsBS!IDM, "")
.Range("B" & i).Value = Nz(rsBS!bk, "")
.Range("C" & i).Value = Nz(rsBS!freight, "")
.Range("D" & i).Value = Nz(rsBS!curre, "")
i = i + 1
rsBS.MoveNext
Loop
End With
SubExit:
On Error Resume Next
DoCmd.Hourglass False
xlApp.Visible = True
rsBS.Close
Set rsBS = Nothing
Exit Sub
SubError:
MsgBox "Error Number: " & Err.Number & "= " & Err.Description, vbCritical + vbOKOnly, _
"An error occurred"
GoTo SubExit
End Sub