Column names containing spaces - select statement in sql

dmcgimpsey

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

I am reading a CSV file using ADODB method, and I want to code a select statement that refers to column headings containing blanks.

I tried using square braces around the field name but it did not recognize the select statement. Select * works. I get the names from the rs.Fields(x).name value

I want to code somthing like this:

Select [Field Name] from table

I just need the syntax of the select statement

Thanks in advance

Don



Here is my code:

cN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"
cN.ConnectionTimeout = 40
cN.Open

Set RS = New ADODB.Recordset

sQuery = "Select * From " & filename

RS.ActiveConnection = cN

RS.Source = sQuery
RS.Open


If RS.EOF <> True Then
While RS.EOF = False
Open Outfile & "\LOAD\loadfile.csv" For Append As 1

Print #1, RS.Fields(0) & "," & RS.Fields(1)
RS.MoveNext
Close #1
Wend
End If
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Not sure. It works for me in a with or without spaces:

Select FieldA FROM TestCSV;
Select [Field C] From TestCSV;

You'll have to give more information on what these field names are and so on I think - perhaps a complete code that demonstrates the problem.

ξ

Note: However, spaces don't belong in database field names. Best is to use only letters, numbers, underscores. Always start with a letter. These rules are, as far as I know, compatible across all systems that might handle your data.
 
Upvote 0
Here is the problem: I get these CSV files from an excel worksheet. I already wrote a utility that sweeps a file directory and converts all the excel files into CSV files. Therefore, I am not in control of the names the people put on the column headings. I simply want to convert a number of fields into an access database that already exists, and the original person has put the field names with embedded spaces.

I can use square brackets in Access as follows: Select [Field a] from table ;

I want to know what the frame or delineator is when reading a csv file. if I get the field names that are in the record set, they do contain spaces.

Thanks

Don
 
Upvote 0
Well, as I wrote above, brackets worked for me. Can you provide more information about the exact error you are getting and when it occurs. Ideally, a test case that demonstrates the issue - the code you are using and the sample CSV file with at least a few rows of data.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,578
Messages
6,179,650
Members
452,934
Latest member
mm1t1

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