Results 1 to 11 of 11

Thread: can u say how to write scripts in Ms Access?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    can u say how to write scripts in Ms Access?

    Hi All,
    i have two databases. one is SQL server 2000 and another one is MS-Access.
    initially I have same data in both the data bases. but now i made some changes in Sql Data base by writing some scripts (script contains sime update,select,insert statements and if clause.
    I want to do the same changes on Access data base also. is it possible to write the scripts in Access-2003?
    I tried with a simple example. i wrote an insert statement in a .txt file. now i am reading that query using "Module" in the Access. following is the example written in "Module" of Access Databse.

    Code:
      
         Option Compare Database
    
      Sub macro1()
           Dim sFileText As String
           Dim strQuery As String
           iFileNo = FreeFile
           Open "c:\sample1.txt" For Input As #iFileNo
           Do While Not EOF(iFileNo)
               Input #iFileNo, sFileText
               strQuery = Replace(sFileText, "99", ",")  '--it replaces 99 with ,
              DBEngine(0)(0).Execute strQuery
           Loop
     End sub
    since, while reading the data from the file, it is not accepting "," (comma), i used "99" instead of "comma" and replaced it again in the string.
    for this simple statement, it is working fine. but it is not working for "if" statement ...etc.

    so can u suggest me any method to write the scrits in Access 2003? my requirement is i should be able to update the Access database with minimum efforts by using the scripts what i alreay used for SQL.

    thanks in advance...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: can u say how to write scripts in Ms Access?

    What "If" statement isn't it working for?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: can u say how to write scripts in Ms Access?

    It's a strange post there, each time I read it I keep interpreting it as a different question. From what I gather, he has a SQL statement in c:\sample1.txt. He wants to read from that text file, into a string, and then execute that string (which is an INSERT statement) against the database.

    Did I get that right?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u say how to write scripts in Ms Access?

    Yes mendhak. you are right. i want to read the sql statements from the text file and execute those statements in Access. is it possible for if
    and select statements?

    Quote Originally Posted by mendhak
    It's a strange post there, each time I read it I keep interpreting it as a different question. From what I gather, he has a SQL statement in c:\sample1.txt. He wants to read from that text file, into a string, and then execute that string (which is an INSERT statement) against the database.

    Did I get that right?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: can u say how to write scripts in Ms Access?

    An SQL query is simply a string, so that is easy enough, but, IF statements are VB specific conditional coding statements that you are not going to be able to execute as IF they were a string.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u say how to write scripts in Ms Access?

    hi Hack,
    then can i use any record set in module? if so do you have any idea about syntax?

    thanks...


    Quote Originally Posted by Hack
    An SQL query is simply a string, so that is easy enough, but, IF statements are VB specific conditional coding statements that you are not going to be able to execute as IF they were a string.

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: can u say how to write scripts in Ms Access?

    Can you post your actual query that you put in the text file?
    You cannot use IF in Access query but you can use IIf().

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: can u say how to write scripts in Ms Access?

    Is the sql statement for the SQL database or the access database? There is some differences betwen the two.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u say how to write scripts in Ms Access?

    Hi,
    the sql statement is for Access database. and in text file the query will be like this....
    Code:
    if ((select Marks from student where stuName ='Robert') < 50)
          Delete student where stuName='Robert'
    Here STUDENT is a table.

    Thanks.
    Quote Originally Posted by RobDog888
    Is the sql statement for the SQL database or the access database? There is some differences betwen the two.

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: can u say how to write scripts in Ms Access?

    I wonder how can you handle that 2 databases but don't know to write this simple query !!!

    To delete all student records with Marks < 50:
    Code:
    DELETE student.* FROM student WHERE (student.Marks < 50)
    To delete student Robert record if his Marks < 50:
    Code:
    DELETE student.* FROM student WHERE (student.Marks < 50) And (student.stuName = 'Robert')

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: can u say how to write scripts in Ms Access?

    Hi anhn,
    sorry for the delay.

    I know how to write a delete statement. the code in my above post is an example for how i am using the IF statement. In my real application the table names, fields and the queries are different. (if i write the original query, it may confuse you so i wrote a simple if statement).

    now i am using VB application with ADO dynamic objects to run the queries.
    i am executing the queries as below

    Code:
    rstRecordSet.Open strSql, conConnection, adOpenStatic, adLockBatchOptimistic
    as our friends told in above posts....., it may not be possible to use IF statements in Queries....
    If it is possible, please let me know it.
    Thanks in advance...

    [QUOTE=anhn]I wonder how can you handle that 2 databases but don't know to write this simple query !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width