mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Query Access Database with VBScript

Author Message
TomRiddle

  • Total Posts : 608
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
  • Status: offline
Query Access Database with VBScript Friday, April 11, 2008 2:35 PM (permalink)
0
This is my tutorial on how to build a query that you can use in VBScript to extract information from a database.

The level of difficulty for this tutorial is beginner to intermediate.
You need to know basic VBScript syntax and need to have a copy of MS Access.

Pay attention to the SQL_query parts in the following blocks of code.


This is the code syntax for ASP

Note this code is striped down to the most basic HTML for demonstration purposes.
 
    <!--demo.asp-->
 
    <html>
    <Title> Read Database Tutorial - Using a query you built yourself </Title>
    <body>
 
    <h2>Total Jobs Each User - Sorted by Largest Total First.</h2><br>
 
    <TABLE border="1">
       <TR>
          <TD><B>User</a></B></TD>
          <TD><B>Total</a></B></TD>
       </TR>
 
    <%
    Set MyConn = Server.CreateObject("ADODB.Connection")
    MdbFilePath = Server.MapPath("PrinterCounts.mdb")
    MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
 
  
    SQL_query = "SELECT First([PrnCounts].[DomainUser]) AS FirstOfDomainUser, Sum([PrnCounts].[NumberPages]) AS SumOfNumberPages "&_
                   "FROM PrnCounts "&_
                      "GROUP BY [PrnCounts].[DomainUser] "&_
                         "HAVING (((Count(PrnCounts.DomainUser))>1 Or (Count(PrnCounts.DomainUser))=1)) "&_
                            "ORDER BY Sum([PrnCounts].[NumberPages]) DESC;"
  
    Set RS = MyConn.Execute(SQL_query)
    WHILE NOT RS.EOF
    %>
 
 
    <TR>
       <TD><%=RS("FirstOfDomainUser")%></TD>
       <TD><%=RS("SumOfNumberPages")%></TD>
    </TR>
 
  
    <%
    RS.MoveNext
    WEND
 
    RS.Close
    set RS = nothing  
    MyConn.close
    set MyConn = nothing
    %>
 
 
    </TABLE>
  
    </body>
    </html>
 
 
 


This is the Code Syntax for WSH.

Notice I have removed instances of "Server." and "MapPath" from the script.
I have also removed the HTML stuff.
 
    'demo.vbs
    
    wscript.echo "Total Jobs Each User - Sorted by Largest Total First"
 
    Set MyConn = CreateObject("ADODB.Connection")
    MdbFilePath = "PrinterCounts.mdb"
    MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"
 
  
    SQL_query = "SELECT First([PrnCounts].[DomainUser]) AS FirstOfDomainUser, Sum([PrnCounts].[NumberPages]) AS SumOfNumberPages "&_
                   "FROM PrnCounts "&_
                      "GROUP BY [PrnCounts].[DomainUser] "&_
                         "HAVING (((Count(PrnCounts.DomainUser))>1 Or (Count(PrnCounts.DomainUser))=1)) "&_
                            "ORDER BY Sum([PrnCounts].[NumberPages]) DESC;"
  
    Set RS = MyConn.Execute(SQL_query)
    WHILE NOT RS.EOF
       Wscript.echo "User" & RS("FirstOfDomainUser") &_
                 " Count" & RS("SumOfNumberPages")
       RS.MoveNext
    WEND
 
    RS.Close
    set RS = nothing  
    MyConn.close
    set MyConn = nothing
 
 


Did you understand all that?? Don't worry if you didn't, because you don't need to understand it.
These blocks of code seem complex but all you have to understand is that they are templates with an SQL query.

There are many different examples of creating databases and tables, and reading, writing and deleting records on the Internet.
The only hard thing is the SQL query itself but I will now show you how I easily get the query statement from MS Access.

(These screen shots were taken from Access 2000)



For a full example of the possibilities of this script, plus code that creates the database and populates_
it with data from the print server's event logs can be found here.
http://visualbasicscript.com/m_58935/tm.htm
<message edited by TomRiddle on Friday, April 11, 2008 11:02 PM>
Attached Image(s)
#1
    ehvbs

    • Total Posts : 3312
    • Scores: 110
    • Reward points : 0
    • Joined: 6/22/2005
    • Location: Germany
    • Status: offline
    RE: Query Access Database with VBScript Friday, April 11, 2008 6:37 PM (permalink)
    0
    Hi TomRiddle,

    thanks for the nice tutorial. One nit to pick:

    This:
      
       WEND
       MyConn.close
       set RS = nothing

    should be:                                                          or:

       WEND                                                              WEND
       RS.Close                                                          RS.Close
       MyConn.close                                                  set RS = nothing  ' optional
       set RS = nothing  ' optional                             MyConn.close
       set MyConn = nothing  ' optional                    set MyConn = nothing  ' optional

    Regards

    ehvbs


    #2
      TomRiddle

      • Total Posts : 608
      • Scores: 12
      • Reward points : 0
      • Joined: 2/7/2008
      • Location: Australia
      • Status: offline
      RE: Query Access Database with VBScript Friday, April 11, 2008 11:09 PM (permalink)
      0
      Thanks for the tip ehvbs, I updated the original post.
      Always learning.
      #3
        cadman75

        • Total Posts : 1
        • Scores: 0
        • Reward points : 0
        • Joined: 3/17/2010
        • Status: offline
        Re:Query Access Database with VBScript Wednesday, March 17, 2010 3:10 AM (permalink)
        0
        I would like to see your linked post, but the link no longer works. Could you repost the script to create the database?

        Sean
        #4
          TomRiddle

          • Total Posts : 608
          • Scores: 12
          • Reward points : 0
          • Joined: 2/7/2008
          • Location: Australia
          • Status: offline
          Re:Query Access Database with VBScript Friday, March 26, 2010 9:18 PM (permalink)
          -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.8
            mbt shoes www.wileywilson.com