aragorn54915
New member
- Local time
- Today, 15:54
- Joined
- Feb 20, 2009
- Messages
- 3
I have a form that displays records based upon user defined values. This form has a button that allows the user to send the data to an Excel file. This works great until the query uses LIKE in the select statement. The only data that is showing up in the Excel spread sheet are the field names. From the tests that I have run, it appears that the Do Until rsNA.EOF Loop is being skipped. Does anyone have any ideas on why this section of code would be skipped when using a query that uses the LIKE statement?
Code:
Dim rsNA As ADODB.Recordset
Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim strSQLNA As String
Dim col, row, rsCount As Integer
Dim fld As ADODB.Field
Dim EmpID As String
EmpID = Environ$("Username")
' Connect to Excel
Set objExcel = New Excel.Application
' Open workbook
objExcel.Workbooks.Open "C:\Documents and Settings\b64365\Desktop\Global Spend\Template.xlsm"
'Open worksheet
Set objSheet = objExcel.ActiveSheet
objExcel.Visible = True
strSQLNA = "SELECT * FROM " & EmpID & "qryNAPOOrdersFiltered"
Set rsNA = New ADODB.Recordset
rsNA.Open strSQLNA, CurrentProject.Connection
For col = 0 To rsNA.Fields.Count - 1
Set fld = rsNA.Fields(col)
objSheet.Cells(4, col + 1) = fld.Name
objSheet.Cells(4, col + 1).Font.Bold = True
Next col
' NA data sent to Excel
row = 5
Do Until rsNA.EOF
With objSheet
.Cells(row, "A").NumberFormat = "@"
.Cells(row, "B").NumberFormat = "@"
.Cells(row, "E").NumberFormat = "@"
.Cells(row, "F").NumberFormat = "@"
.Cells(row, "G").NumberFormat = "@"
.Cells(row, "H").NumberFormat = "@"
.Cells(row, "I").NumberFormat = "@"
.Cells(row, "K").NumberFormat = "@"
.Cells(row, "L").NumberFormat = "@"
.Cells(row, "M").Style = "Currency"
.Cells(row, "P").Style = "Currency"
End With
For col = 0 To rsNA.Fields.Count - 1
objSheet.Cells(row, col + 1) = rsNA.Fields(col).Value
Next col
rsNA.MoveNext
row = row + 1
Loop
For col = 0 To rsNA.Fields.Count - 1
objSheet.Columns(col + 1).EntireColumn.AutoFit
Next col
rsNA.Close
Set rsNA = Nothing