So I'm trying to put together a function that will read a couple of select boxes (can be multiple), will then convert to SQL query and open a recordset to an Access database and put the results into a csv.
I have tried putting bits and pieces together that I have found on the net but I'm getting an object expected error. Here is my code so far:
Sub writeCSV()
Dim strSQL
Dim objAccessConnection
Dim strAccessQuery
Dim objAccessRecordset
Dim strLevel
Dim strCountries
Dim intLength
For Each item In Request.Form("level")
strLevel = strLevel & Response.Write(Request.Form(item)) & ","
Next
intLength = Len(strLevel)
strLevel = Left(strLevel, intLength - 1)
For Each item In Request.Form("countries")
strCountries = strCountries & Response.Write(Request.Form(item)) & ","
Next
intLength = Len(strCountries)
strCountries = Left(strCountries, intLength - 1)
strSQL = "SELECT fName, fCity, fCountry, fEmai1, fType FROM tblAgents WHERE fType IN (" & strLevel & ") AND fCountry IN (" & strCountries & ")"
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adClipString = 2
Const ForWriting = 2
Const ForAppending = 8
Const strDB = "H:\Database.mdb"
Const strCSV = "C:\ExportAgentEmails.csv"
Set objAccessConnection = CreateObject("ADODB.Connection")
objAccessConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & strDB
Set objAccessRecordset = CreateObject("ADODB.Recordset")
strAccessQuery = strSQL
objAccessRecordset.Open strAccessQuery, objAccessConnection, adOpenStatic, adLockOptimistic
Set objCSV = CreateObject("Scripting.FileSystemObject").OpenTextFile(strCSV, ForAppending, True)
objCSV.Write objAccessRecordset.GetString(adClipString,,",",CRLF)
objCSV.Close
Set objCSV = Nothing
objAccessRecordset.Close
Set objAccessRecordset = Nothing
objAccessConnection.Close
Set objAccessConnection = Nothing
End Sub
...And the form...
If someone could tell me where I'm going wrong? Otherwise if you have a Javascript version I could use that also.
Thanks all.