开发者

Access MDB VBA Query not working

I am trying to connect to a access database file in VBA and create a table but i cannot seem to be able to query back the values after I have inserted them. For i开发者_JAVA技巧nstance, the query "SELECT * FROM TESTME WHERE 'Portfolio' = 'FALL' is always returning back no records. I have even tried "While NOT RS.EOF" but its always true. Any ideas what's wrong with my syntax? Thanks.

Sub CreateTblinDb()
On Error GoTo errhandler:
Dim scon As String
Dim ssql As String

scon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Jet OLEDB:Engine Type=" & Jet4x & _
  ";Data Source=" & "C:\a2000.mdb"
'creates a table in the database

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

ssql = "CREATE TABLE TESTME2 ('Portfolio' memo, 'TradeType' memo)"

Set cn = New ADODB.Connection

cn.Open scon
cn.Execute ssql

ssql = "INSERT INTO TESTME2 VALUES('FALL','TEST')"
cn.Execute ssql

ssql = "SELECT * FROM TESTME2 WHERE 'Portfolio' = 'FALL'"

Set rs = New ADODB.Recordset

rs.Open ssql, cn, adOpenKeyset, adLockOptimistic
Debug.Print rs.RecordCount ' ALWAYS RETURNING 0

Exit Sub

errhandler: MsgBox Err.Description End Sub


'Portfolio' and 'FALL' are two different strings and will never equal each other; not even on June 23rd, 2011 at 6:29pm.

What's more, your table definition statement does not look correct for a similar reason.

Edit:
Since backticks are not ANSI SQL, there a couple of other things to try:

  1. brackets, or
  2. double-quotes.

After you fix the table definition statement you might try this query:

SELECT * FROM TESTME2 WHERE [Portfolio] = 'FALL'

Edit:
Example DDL by request...

CREATE TABLE TESTME2 ([Portfolio] memo, [TradeType] memo)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜