how to escape the ' in VBA

RompStar

Well-known Member
Joined
Mar 25, 2005
Messages
1,200
I know that this character is used for comments in VBA: '

but I have a need to use it in my code, here is some simple code to simulate my situation and what I am trying to accomplish. Basically I am working with the Oracle driver to connect to the database, execute a series of different queries and then have the data be pasted back in Excel for final analysis.

When I execute this code, I get this:

SELECT COLUMN1, COLUMN2 FROM TABLE WHERE VERSION=10_A;

but I need this:

SELECT COLUMN1, COLUMN2 FROM TABLE WHERE VERSION='10_A';

because the VERSION column is a VARCHAR2 field and I need the tickmarks around the value to select. How do I employ the ' without having everything turn into comments ?



Sub Macro1()

iVersion = "10_A"

Sql_Main_Table = "SELECT COLUMN1, COLUMN2 FROM TABLE WHERE VERSION=" & iVersion & ";"

Sheets("Sheet1").Range("A1").Value = Sql_Main_Table

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about:

Code:
Sql_Main_Table = "SELECT COLUMN1, COLUMN2 FROM TABLE WHERE VERSION='" & iVersion & "';"
 
Upvote 0
or this:
Code:
Sub Macro1()
iVersion = "'10_A'"
Sql_Main_Table = "SELECT COLUMN1, COLUMN2 FROM TABLE WHERE VERSION=" & iVersion & ";"
Sheets("Sheet1").Range("A1").Value = Sql_Main_Table
End Sub
 
Upvote 0
You'd use the Char() function. Sorry, don't remember the number for ', but once you find it, your statement would look like this:

dim sql as string
sql = "SELECT COLUMN1, COLUMN2 FROM TABLE WHERE VERSION=" & Chr(10) & "10_A" & Chr(10) & ";"
 
Upvote 0
I was getting error run-time 440, for some strange reason, it don't like the ; at the end,
but in SQL world that is used to end the Query, weird, I removed it and it works now :- )

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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