ADO SQL - CSV Delimiter specifying

dmcgimpsey

Active Member
Joined
Mar 30, 2004
Messages
268
Hi Folks:

Can someone give me a quick lesson on ADO SQL?

I need to specify the delimiter in the CSV connection....

Here is the code, shamelessly cut and pasted from examples;

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFilePath & ";" & _
"Extended Properties=""text;HDR=Yes;FMT=Delimited"""

Thanks in advance....
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Thanks for the quick reply - I have tried to do that but when I check on the field names in the select statement results, I get all the fields glued to each other with the delimiter embedded. This doesn't seem to recognize the FMT=Delimited(|) options.
 
Upvote 0
Re: ADO SQL - CSV Delimiter specifying - STILL NOT WORKING

I have not been able to resolve this issue as of yet:

The file works fine when I use comman delimiters but since there is embedded commas in the freeform text, I need to use a different delimiter.

What happens is this:

I select * from the table and when I loop through the field names, all of the fields are colapsed into the first column. Therefore, it is not recognizing the delimiter.

Here is the exact code

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFilePath & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited(|)"""


Thanks
 
Upvote 0
hi what delimiter are you trying to use, I have just been through the exercise last week so the ideas are quite fresh
 
Upvote 0
here is my complete code for extracting data from a CSV file which is delimited with a semi colon, it extracts the data and builds a report to email to each group, you can edit out the bits you dont require

the way it gets around the delimiter problem is to create a file called schema.ini in the same folder as the data file which describes the format and also the fields, I have given them different names for my process different to the column headers in the original file. You will see the format of the file in the routine createSchema, i just create it each time as this is used from outlook by many people

My process first queries the file and extracts all of the unique actiongroups and then loops thru and finds all records for each actiongroup, with a second call to ADO

you can remove the outlook bit and pull whatever columns straight into excel, cant remember the syntax but found it on my travels, will update when I find it, was a simple one liner

here is my code from top to bottom

'****
'
' Author Jim Ward
' Creation 5th June 2009
'
' Procedure to hopefully go someway to automating the Monday event of
' Notification of Suspended Incidents sent to a group of people
'
' Currently the exercise is manual and requires quite a bit of time
'
' Here is the plan
'
' When the email is received from Easy Vista, click on the link given
' at the top of the web page opened, click on the export to spreadsheet button and
' choose to save to my document as the name suggested
'
' the routine below will look for the file and rename it to a shorter name the ADODB
' function can handle.
'
' it will then open the file and look for a unique list of groups to report
' next it will take each of these groups in turn and return all records for that group SORTED
' using this it will create an email for the group and address it to the correct recipient
'
' IT WILL NOT SEND THE EMAIL, because of limitation I cannot be bothered working around, it will
' not add IT SERVICE DESK as the sender, this can be done manually and the contents can be visually
' checked for accuracy, then click on SEND
'
'****
' Declare some global variables
'****

Public IntAddress As Integer
Public StrAddressBook(10, 1) As String
Public StrAddressFile As String

Public StrPathToTextFile As String

Sub EmailNotificationSuspended()

'****
' Setup constants for the SQL query
'****

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1

'****
' Declare stuff
'****

Dim IntGroupCount As Integer
Dim IntNextGroup As Integer
Dim StrGroup As String
Dim StrGroups(10) As String

Dim StrFile As String
Dim StrMonster As String

Dim StrSQL As String
Dim StrMailBody As String
Dim StrSignature As String

'****
' create objects that we need
'****

Set objConnection = CreateObject("ADODB.Connection")
Set ObjRecordSet = CreateObject("ADODB.Recordset")

'****
' Rename the EV monster filename to something ADODB can handle
' it does not like long names, check it exists first
'****

StrPathToTextFile = MyDocuments()

StrMonster = "\Automatic_Notification_of_suspended_Incidents_due_for_reactivation.csv"
If Not FileExists(StrMonster) Then
MsgBox ("Please follow the link in the email from Easy Vista and save the file to My Documents using the default filename suggested")
Exit Sub
End If

StrFile = "\Automatic.csv"
Name StrPathToTextFile & StrMonster As StrPathToTextFile & StrFile

'****
' create schema file, may become a file copy in the future
'****

Call CreateSchema

'****
' Load address book from text file, again maybe a filecopy in the future to keep one master list
'****

If Not FileExists("\Notify_Emails.txt") Then
MsgBox ("You do not have address file NOTIFY_EMAILS.TXT contact IT Service Desk, save the file to My Documents")
Exit Sub
End If
Call LoadAddressBook

'****
' create the standard signature common to all emails
'****

StrSignature = BuildSignature()

'****
' All preparatory work done, lets begin the main process
'****

'****
' Open JET connection to our data file
'****

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & StrPathToTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""

'****
' Build SQL strings to get unique categories in no order
'****

StrSQL = "SELECT DISTINCT ActionGroup FROM " & StrFile

'****
' Execute the SQL and get the results and store for next stage
'****

ObjRecordSet.Open StrSQL, objConnection, adOpenStatic, adLockOptimistic, adCmdText

IntGroupCount = 0
Do Until ObjRecordSet.EOF
StrGroups(IntGroupCount) = ObjRecordSet.Fields.Item("ActionGroup")
IntGroupCount = IntGroupCount + 1
ObjRecordSet.MoveNext
Loop
IntGroupCount = IntGroupCount - 1
ObjRecordSet.Close

'****
' Build SQL strings to get all records for each category in sorted order
'****

For IntNextGroup = 0 To IntGroupCount
StrSQL = "SELECT * FROM " & StrFile
StrSQL = StrSQL & " WHERE ActionGroup Like '" & StrGroups(IntNextGroup) & "%' order by SUPPORT, LATESTDATE"

'****
' Execute the SQL, get the results and build our email body string
'****

ObjRecordSet.Open StrSQL, objConnection, adOpenStatic, adLockOptimistic, adCmdText

StrMailBody = "Incident Number Date Analyst" + vbCrLf

Do Until ObjRecordSet.EOF
a = ObjRecordSet.Fields.Item("ActionGroup")
b = ObjRecordSet.Fields.Item("IncidentNumber")
c = ObjRecordSet.Fields.Item("Support")
d = ObjRecordSet.Fields.Item("LatestDate")
d = Format(d, "dd/mm/yyyy")
StrMailBody = StrMailBody + b + vbTab + CStr(d) + vbTab + c + vbCrLf
ObjRecordSet.MoveNext
Loop
ObjRecordSet.Close

'****
' Create a new email for each group of interest
'****

Dim msg As Outlook.MailItem
Set msg = Application.CreateItem(olMailItem)
msg.To = FindAddress(StrGroups(IntNextGroup))
msg.Subject = "Automatic Notification of Suspended Incidents Due For Reactivation :- " + StrGroups(IntNextGroup) + " :- " + Format(Now, "yyyymmdd")
msg.Body = StrMailBody & vbCrLf & vbCrLf & StrSignature
msg.Display
Set msg = Nothing
Next

End Sub
Function MyDocuments() As String
Dim WSHShell As Object
Set WSHShell = CreateObject("Wscript.Shell")
MyDocuments = WSHShell.SpecialFolders("MyDocuments")
Set WSHShell = Nothing
End Function
Function FindAddress(StrActionGroup As String) As String
For j = 0 To IntAddress
If StrAddressBook(j, 0) = "[" & UCase(StrActionGroup) & "]" Then
FindAddress = StrAddressBook(j, 1)
Exit For
End If
Next j
End Function
Private Function FileExists(fname) As Boolean
' Returns TRUE if the file exists
Dim x As String
x = Dir(StrPathToTextFile & fname)
If x <> "" Then FileExists = True _
Else FileExists = False
End Function
Function BuildSignature() As String

BuildSignature = "IT Service Desk" & vbCrLf


End Function


Sub CreateSchema()

Open StrPathToTextFile & "\schema.ini" For Output As #1

Print #1, "[Automatic.csv]"
Print #1, "Format=Delimited(;)"
Print #1, "ColNameHeader=True"
Print #1, "MaxScanRows=0"
Print #1, "Col1=IncidentNumber Text"
Print #1, "Col2=ActionGroup Text"
Print #1, "Col3=Support Text"
Print #1, "Col4=LatestDate DateTime"

Close #1

End Sub
Sub LoadAddressBook()

Open StrPathToTextFile & "\Notify_Emails.txt" For Input As #1

IntAddress = 0
Do While Not EOF(1)
Input #1, StrAddressBook(IntAddress, 0)
Input #1, StrAddressBook(IntAddress, 1)
IntAddress = IntAddress + 1
Loop

IntAddress = IntAddress - 1
Close #1 ' Close file.

End Sub
 
Upvote 0
where you see the smiley in the CreateSchema routine this is in fact a semicolon, the forum board is changing it to a smiley
 
Upvote 0
So much thanks, the link to the MS ADO article (blog) was very useful, I was indeed able to build the schema.ini file on the fly and the application now works fine.

Thanks to everyone for helping me out ....

.... tag-team programming !!! Yay !!!
 
Upvote 0
i started with the MS article and it made so many assumptions, like you knew what you were doing and err why would you be at the page

my only problem I came across was I called one of my fields group in the schema and cos its a reserved word, the select did not want to play, elementary error on my part

did you find the one liner for inserting your select array of data into excel
 
Upvote 0

Forum statistics

Threads
1,220,951
Messages
6,157,030
Members
451,392
Latest member
malcv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top