Hi,
I am trying to write a macro to read the database, and here it is. define !read_db(db_path=!charend('/') / tbl=!charend('/')) GET DATA /TYPE=ODBC /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!db_path,";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) /SQL=!quote(!concat('SELECT * FROM ',!tbl)) /ASSUMEDSTRWIDTH=255. CACHE. EXECUTE. !enddefine. !read_db db_path= c:\test.accdb / tbl=test/. The above code works, but I have to specify the full path every time I use this marco. Is there a way to declare a global constant variable so that I could use? I try to use the following declaration, but it does not work. FILE HANDLE db /NAME="c:\test.accdb". !read_db db_path= db / tbl=test/. Any help? |
Administrator
|
"but it does not work."
... Please define "but it does not work." -------------------------------------- Error msgs? I WILL NOT try to run your code to attempt to sort out your issues without specifics!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Hi, The error message is below:******************************************************** set mprint on. FILE HANDLE db /NAME="c:\test.accdb". 12 0 M> FILE HANDLE db /NAME="c:\test.accdb". 13 0 M> 14 0 M> define !read_db(db_path=!charend('/') / tbl=!charend('/')) 15 0 M> define !read_db(db_path=!charend('/') / tbl=!charend('/')) 16 0 M> GET DATA 17 0 M> GET DATA /TYPE=ODBC 18 0 M> /TYPE=ODBC /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!db_path,";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) 19 0 M> /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!db_path,";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) /SQL=!quote(!concat('SELECT * FROM ',!tbl)) 20 0 M> /SQL=!quote(!concat('SELECT * FROM ',!tbl)) /ASSUMEDSTRWIDTH=255. 21 0 M> /ASSUMEDSTRWIDTH=255. CACHE. 22 0 M> CACHE. EXECUTE. 23 0 M> EXECUTE. !enddefine. 24 0 M> !enddefine. !read_db db_path= db / tbl=test/. 25 0 M> 26 0 M> . 27 0 M> GET DATA /TYPE=ODBC /CONNECT= 'DSN=Access2007;DBQ=db;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;' /SQL= 'SELECT * FROM test' /ASSUMEDSTRWIDTH=255. >Warning. Command name: GET DATA >SQLDriverConnect failed :[Microsoft][ODBC Microsoft Access Driver] Not a valid file name. 28 0 M> CACHE. 29 0 M> EXECUTE >Error # 105. Command name: EXECUTE >This command is not valid before a working file has been defined. >Execution of this command stops. 30 0 M> . ******************************************************** On Thu, Jul 18, 2013 at 2:54 PM, David Marso [via SPSSX Discussion] <[hidden email]> wrote: "but it does not work." |
Actually, the error message is very clear.
GET DATA /TYPE=ODBC /CONNECT= 'DSN=Access2007;DBQ=db;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;' /SQL= 'SELECT * FROM test' /ASSUMEDSTRWIDTH=255. DBQ=db --> db is not a valid path define !db () 'c:/temp/someDatabase.mdb' !enddefine. define !tbl () 'tblSomeTable' !enddefine. GET DATA /TYPE=ODBC /CONNECT= 'DSN=Access2007;DBQ=' + !db + ';DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;' /SQL= 'SELECT * FROM ' + !tbl + ' /ASSUMEDSTRWIDTH=255.' Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >________________________________ >From: albert_sun <[hidden email]> >To: [hidden email] >Sent: Thursday, July 18, 2013 7:00 AM >Subject: Re: [SPSSX-L] SPSS Macro help > > > >Hi, > >The error message is below: > >******************************************************** >set mprint on. >FILE HANDLE db /NAME="c:\test.accdb". > 12 0 M> FILE HANDLE db /NAME="c:\test.accdb". > > 13 0 M> > > 14 0 M> >define !read_db(db_path=!charend('/') / tbl=!charend('/')) > 15 0 M> define !read_db(db_path=!charend('/') / tbl=!charend('/')) > > 16 0 M> >GET DATA > 17 0 M> GET DATA > /TYPE=ODBC > 18 0 M> /TYPE=ODBC > /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!db_path,";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) > 19 0 M> /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!db_path,";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) > /SQL=!quote(!concat('SELECT * FROM ',!tbl)) > 20 0 M> /SQL=!quote(!concat('SELECT * FROM ',!tbl)) > /ASSUMEDSTRWIDTH=255. > 21 0 M> /ASSUMEDSTRWIDTH=255. >CACHE. > 22 0 M> CACHE. >EXECUTE. > 23 0 M> EXECUTE. >!enddefine. > 24 0 M> !enddefine. >!read_db db_path= db / tbl=test/. > 25 0 M> > 26 0 M> . > 27 0 M> GET DATA /TYPE=ODBC /CONNECT= 'DSN=Access2007;DBQ=db;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;' /SQL= > 'SELECT * FROM test' /ASSUMEDSTRWIDTH=255. > >>Warning. Command name: GET DATA >>SQLDriverConnect failed :[Microsoft][ODBC Microsoft Access Driver] Not a valid file name. > 28 0 M> CACHE. > 29 0 M> EXECUTE > >>Error # 105. Command name: EXECUTE >>This command is not valid before a working file has been defined. >>Execution of this command stops. > 30 0 M> . >******************************************************** >Seems to me, even if I define db as "c:\test.accdb", it is not carried when reading the DBQ (it is db instead of "c:\test.accdb"). > > > > > > >On Thu, Jul 18, 2013 at 2:54 PM, David Marso [via SPSSX Discussion] <[hidden email]> wrote: > >"but it does not work." >>... >>Please define "but it does not work." >>-------------------------------------- >>Error msgs? >>I WILL NOT try to run your code to attempt to sort out your issues without specifics! >> >> >>albert_sun wrote >>>Hi, >>> >>>I am trying to write a macro to read the database, and here it is. >>> >>>define !read_db(db_path=!charend('/') / tbl=!charend('/')) >>> >>>GET DATA >>> /TYPE=ODBC >>> /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!db_path,";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) >>> /SQL=!quote(!concat('SELECT * FROM ',!tbl)) >>> /ASSUMEDSTRWIDTH=255. >>>CACHE. >>>EXECUTE. >>>!enddefine. >>>!read_db db_path= c:\test.accdb / tbl=test/. >>> >>> >>>The above code works, but I have to specify the full path every time I use this marco. Is there a way to declare a global constant variable so that I could use? I try to use the following declaration, but it does not work. >>> >>> >>>FILE HANDLE db /NAME="c:\test.accdb". >>>!read_db db_path= db / tbl=test/. >>> >>> >>>Any help? >>Please reply to the list and not to my personal email. >>Those desiring my consulting or training services please feel free to email me. >>--- >>"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." >>Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" >> >> >>>>________________________________ >> >>If you reply to this email, your message will be added to the discussion below:http://spssx-discussion.1045642.n5.nabble.com/SPSS-Macro-help-tp5721236p5721238.html >>To unsubscribe from SPSS Macro help, click here. >>NAML > > >>________________________________ >View this message in context: Re: SPSS Macro help >Sent from the SPSSX Discussion mailing list archive at Nabble.com. > > > ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Hi, Thanks for your reply. However, the !db part does not work, and the value defined in macro did not pass into. The tbl value is passed in though. See error message.918 0 M> GET DATA /TYPE=ODBC /CONNECT= 'DSN=Access2007;DBQ=!db;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;' /SQL= 'SELECT * FROM test' /ASSUMEDSTRWIDTH=255. >Warning. Command name: GET DATA >SQLDriverConnect failed :[Microsoft][ODBC Microsoft Access Driver] Not a valid file name. 919 0 M> CACHE. 920 0 M> EXECUTE >Error # 105. Command name: EXECUTE >This command is not valid before a working file has been defined. >Execution of this command stops. 921 0 M> . On Thu, Jul 18, 2013 at 5:46 PM, Albert-Jan Roskam [via SPSSX Discussion] <[hidden email]> wrote:
|
Administrator
|
Please confirm that the expanded syntax works outside of a macro!!!!!!!
I suspect an epic fail and back to the drawing board!!!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Finally, made this work by revising the syntax. Thanks ALbert_Jan for the advise.define !db () c:/test.accdb !enddefine. define !tbl () tbltest !enddefine. define !read () GET DATA /TYPE=ODBC /CONNECT=!quote(!concat("DSN=Access2007;DBQ=",!eval(!db),";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")) /SQL=!quote(!concat('SELECT * FROM ',!eval(!tbl))) /ASSUMEDSTRWIDTH=255. CACHE. EXECUTE. !enddefine.
On Fri, Jul 19, 2013 at 11:59 AM, David Marso [via SPSSX Discussion] <[hidden email]> wrote: Please confirm that the expanded syntax works outside of a macro!!!!!!! |
Administrator
|
Yes, !EVAL fits the bill for resolving macro defs!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |