Solved

Microsoft Access VBA question -- Same problem

Posted on 2009-07-08
6
437 Views
Last Modified: 2013-12-06
A question from user Jeanniem has not been actually answered, and i noticed that it is closed.
I'm having the same problem  (I'm using msaccess 2003)

  Can't construct a simple SQL on MS-ACESS VB. I'm using ODBC on an AS400.
   I'm having the same runtime error as Jeanniem.

   For some reason it is not permitting the use of SELECT Clause.

   However it is accepting this string while i'm experimenting.

   SQLStr = "LIB.Table1, LIB.Table2 Where Table1.FLD1 = 1 And Table1.FLD1 = Table2.FLD2"

  This query is accepted. I think it is assuming that it is in (Select *) default. Notice that i put the join in the where clause (a workaround 'And Table1.FLD1 = Table2.FLD2')

   @Jeanniem --> could you try this and give me any feedback? thanks
Set Cnxn = New ADODB.Connection

Set rst1 = New ADODB.Recordset

strCnxn = "Provider='IBMDA400';Data Source='myDSN';User d='';Password='';"

Cnxn.Open strCnxn
 

SQLStr = "Select * From LIB.Table1 Inner join LIB.Table2 On Table1.FLD1 = Table2.FLD2 Where FLD1 = 1"
 

rst1.Open SQLStr, Cnxn, adOpenKeyset, adLockOptimistic, adCmdTable

Open in new window

0
Comment
Question by:belbenj
6 Comments
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 150 total points
Comment Utility
Remove the adCmdTable argument and see what happens.

0
 
LVL 16

Expert Comment

by:Chuck Wood
Comment Utility
1. Is FLD1 a field with the same name in Table1 and Table2? If so, you need to qualify it (Table1.FLD1)
2. Shouldn't it be "Select * From LIB/Table1 Inner join LIB/Table2 ..." for DB2 on AS400?
-chuck wood
0
 

Expert Comment

by:Jeanniem
Comment Utility
When I had this problem, it was solved with help from Gary the IT.....

He suggested I do a debug.print on my SQL statement - when I did that it showed me that I didn't have spaces betweeen some of the words and when I fixed that, it worked.  It looks like your SQLstr is correct, but it wouldn't hurt to do a debug.print just in case.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Accepted Solution

by:
Gary Patterson earned 350 total points
Comment Utility
The IBMDA400 provider defaults to the SQL naming convention (lib.table).  You can switch to System naming convention (lib/table) if you like by adding the "Naming Convention=1" option to your connection string.  It defaults to "0", so your table names are formatted OK here.

As observed by LMSConsulting, adCmdTable option needs to go.  This option causes the ADO provider to generate a query for you, and is not compatible with an SQL statement being provided in the SOURCE parameter.

Now, what is the exact error message that you are getting?

A few additional notes:

1) IBMDA400 is an OLE DB provider.  OLE DB Providers do not use ODBC data sources, so if "myDSN" is an ODBC data source name, you need to change it to the name of your AS/400 (IP address, FQDN, etc.).

2) I don't know if you have any duplicated field names between your tables (are these really the table and field names, or did you substitute generic names?  Post the real field and table names if possible), but it is a good practice to get in the habit of using correlation names for your tables:

Select * From LIB.Table1 as T1 Inner join LIB.Table2 T2 On T1.FLD1 = T2.FLD2 Where T1.FLD1 = 1

Note that each field reference is qualified with the correlation name.

3) If you continue to have trouble, drop down to adOpenStatic and adLockReadOnly and see if your query works.  Post back if you need to change these parameters to make it work (are you planning to update, and do you plan to move bback and forth in your results in a way where you need to see records that change while you are running your application?

4) Test your SQL using the Navigator program that comes with IBM Client Access, navigate to your AS/400 -> Databases -> right click your database, and select Run SQL Scripts.  Test your query here until it works - you'll get easier access to detailed error messages.  Use Connection -> JDBC Setup to set your preferred connection settings (like the naming format).  You can also use the green-screen utility STRSQL to test SQL statements (that is my preferred tool).
 
- Gary Patterson

0
 

Author Comment

by:belbenj
Comment Utility
@Jeanniem: Oops, sory i thought your issue was not resolved. though your last post was "Your definately right about the space, but it still throws the following error". sorry

adCmdTable - is indeed the porblem. thanks. my query is working and i can use joins now. thanks a lot

about "myDSN" -  my bad I'm actually using the IP address not DSN

I also prefer using lib/table format though i don't know where to insert the "Naming Convention=1" parameter in my connection string. can you guys help out? is there any reference docs on all the parameters that can be inserted in the connection string?

myConString = "Provider='IBMDA400';Data Source='XX.XXX.X.XXX';User Id='';Password='';"

Notice that I left User Id and password blank. so that it will pop up the login access to AS400 via client access. but when i click the cancel on the popup. I get msgbox that "user cancelled!" or something. how can i catch that error? thanks all in advance
0
 
LVL 34

Assisted Solution

by:Gary Patterson
Gary Patterson earned 350 total points
Comment Utility
Put it anyplace you like in the connection string.  Order doesn't matter.

OLE DB Driver documentation is included as part of the Help that comes with Client Access Programmer's Toolkit - an optionally-installable Client Access component.

You handle this error like any other in VBA:  Either let the default handler catch it (that is what is happening now), or provide your own error handling.  See the example below.

- Gary Patterson









Set Cnxn = New ADODB.Connection

Set rst1 = New ADODB.Recordset

strCnxn = "Provider='IBMDA400';Data Source='myAS400';User id='';Password='';Naming Convention=1;"
 

On Error Resume Next    'We will handle our own errors

Cnxn.Open strCnxn

If Err.Number = 0 Then

    MsgBox "Connection succeeded"

    Cnxn.Close

Else

    MsgBox "Connection failed with error: " & Err.Number & " - " & Err.Description

    Err.Clear

End If

On Error GoTo 0        ' Switch back to default error handler

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now