populating a javascript array from an access database using asp - vbscript

Posted on 2004-04-23
Last Modified: 2012-06-21

I have a page which connects to an access database using asp / vbscript.

In the head of the page is a javascript for dealing with multiple dynamic drop down menus:

<script type="text/javascript">

var arrStates, arrFix, arrCompactFit, arrSmartCentra, arrSSRange, arrSRange, arrNSRange1, arrNSRange2, arrNSRange3, arrSRRange, arrPSRange, arrS2KRange, arrT2000r1, arrT2000r2, arrT2000r3, arrT2000r4, arrT2003r1, arrT2003r2
arrFix = [ ]
arrMG = ["MG05LED" , "MG07LCD", "MG10LCD"]
arrSSRange = ["SS065", "SS100", "SS150", "SS200", "SS3000"]
arrSRange = ["S1010", "S101007", "S101020", "S101040", "S101050", "S101065", "S101090"]
arrNSRange1 = ["NS1016 1.6kva 0 min", "NS101605 1.6kva 5 min", "NS101615 1.6kva 15 min", "NS101625 1.6kva 25 min", "NS101640 1.6kva 40 min", "NS101655 1.6kva 55 min", "NS101670 1.6kva 70 min", "NS1024 2.4 kva 0 min", "NS102405 2.4 kva 5 min", "NS102415 2.4kva 15 min", "NS102425 2.4kva 25 min", "NS102440 2.4kva 40 min", "NS102455 2.4kva 55 min", "NS102470 2.4kva 70 min", "NS1032 3.2kva 0 min", "NS103205 3.2kva 5 min", "NS103215 3.2kva 15 min", "NS103230 3.2kva 30 min", "NS103240 3.2kva 40 min", "NS103250", "NS103270"]
arrNSRange2 = ["NS1060 6.0kva 0 min", "NS106008 6.0kva 8 min", "NS106025 6.0kva 25 min", "NS106040 6.0kva 40 min", "NS106055 6.0kva 55 min", "NS106075 6.0kva 75 min", "NS1060100 6.0kva 100 min", "NS1060160 6.0kva 160 min", "NS1060240 6.0kva 240 min", "NS1060350 6.0kva 350 min"]
arrNSRange3 = ["NS1080 8.0kva 0 min", "NS108006 8.0kva 6 min", "NS108015 8.0kva 15 min", "NS108030 8.0kva 30 min", "NS108045 8.0kva 45 min", "NS108060 8.0kva 60 min", "NS108120 8.0kva 120 min", "NS108200 8.0kva 200 min", "NS10100 10kva 0 min", "NS10100BB 10kva with battery cab", "NS101008 10kva 8 min", "NS1010020 10kva 20 min", "NS1010040 10kva 40 min", "NS1010050 10kva 50 min", "NS1010080 10kva 80 min", "NS1010150 10kva 150 min", "NS1010240 10kva 240 min"]
arrSRRange = ["SR2010 1.0kva 0 min", "SR201006 1.0kva 6 min", "SR201020 1.0kva 20 min", "SR201035 1.0kva 35 min", "SR201050 1.0kva 50 min", "SR2020 2.0kva", "SR202007 2.0kva 7 min", "SR202020 2.0kva 20 min", "SR202035 2.0kva 35 min", "SR202050 2.0kva 50 min", "SR2030 3.0kva 0 min", "SR203005 3.0kva 5 min", "SR203015 3.0kva 15 min", "SR203030 3.0kva 30 min", "SR203040 3.0kva 40 min", "SR1050 5.0kva 0 min", "SR105010 5.0kva 10 min", "SR105020 5.0kva 20 min", "SR105035 5.0kva 35 min", "SR105060 5.0kva 60 min", "SR1060 6.0kva 0 min", "SR106010 6.0kva 10 min", "SR106020 6.0kva 20 min"]
arrPSRange = ["PS1-2", "PS1-3", "PS1-5", "PS1-8", "PS1-13", "PS2-2", "PS2-3", "PS2-5", "PS2-8", "PS2-13", "PS3-2", "PS3-3", "PS3-5", "PS3-7"]
arrS2KRange = ["S2K080", "S2K08006", "S2K08015", "S2K08030", "S2K08045", "S2K08060", "S2K080120", "S2K080200", "S2K10", "S2K10BB", "S2K10007", "S2K10020", "S2K10030", "S2K10050", "S2K10080", "S2K10150", "S2K10240", "S2K15", "S2K15A", "S2K15B", "S2K15006", "S2K15020", "S2K15035", "S2K15050", "S2K15070", "S2K15120", "S2K15210"]
arrT2000r1 = ["T2K010", "T2K010007", "T2K010020", "T2K010035", "T2K010055", "T2K010075", "T2K010150", "T2K020", "T2K020B", "T2K020008", "T2K020015", "T2K020025", "T2K020035", "T2K020065", "T2K020110", "T2K030", "T2K030B", "T2K030003","T2K030020", "T2K040", "T2K040005", "T2K040008", "T2K040013", "T2K040030", "T2K040050", "T2K040070", "T2K040110"]
arrT2000r2 = ["T2K050", "T2K050006", "T2K050010", "T2K050020", "T2K050035", "T2K050050", "T2K050080", "T2K060", "T2K060003", "T2K060007","T2K060015", "T2K060030", "T2K060035", "T2K060065", "T2K060110", "T2K080", "T2K080003", "T2K080010", "T2K080018", "T2K080030", "T2K080050", "T2K080075", "T2K080110"]
arrT2000r3 = ["T2K100", "T2K100007", "T2K100013", "T2K100018", "T2K100040", "T2K100055", "T2K100085", "T2K100110", "T2K120", "T2K120005", "T2K120010", "T2K120014", "T2K120030", "T2K120045", "T2K120065", "T2K120085", "T2K150", "T2K150007", "T2K150011", "T2K150020", "T2K150035", "T2K150050", "T2K150065"]
arrT2000r4 = ["T2K200", "T2K200003", "T2K200012", "T2K200020", "T2K200035", "T2K200045", "T2K250", "T2K250003", "T2K250009", "T2K250016", "T2K250025", "T2K300", "T2K300006", "T2K300012", "T2K300020", "T2K350", "T2K350005", "T2K350010", "T2K350015", "T2K350020", "T2K400", "T2K400003", "T2K400008", "T2K400012", "T2K400018", "T2K500", "T2K500005", "T2K500009", "T2K500012", "T2600", "T2K600006", "T2K600010"]
arrT2003r1 = ["T3K010", "T3K010B", "T3K010008", "T3K010025", "T3K010040", "T3K010055", "T3K010090", "T3K015", "T3K015B", "T3K015003", "T3K015013", "T3K015025", "T3K015035", "T3K015050", "T3K020", "T3K020B", "T3K020008", "T3K020015", "T3K020025", "T3K020035", "T3K020065", "T3K030", "T3K030B", "T3K030003", "T3K030008", "T3K030012", "T3K030020", "T3K030040", "T3K030065"]
arrT2003r2 = ["T3K040", "T3K040B", "T3K040005", "T3K040008", "T3K040013", "T3K040030", "T3K040050", "T3K050", "T3K050B", "T3K050006", "T3K050010", "T3K050020", "T3K050035", "T3K050050"]

arrStates =[arrFix, arrMG, arrSSRange, arrSRange, arrNSRange1, arrNSRange2, arrNSRange3, arrSRRange, arrPSRange, arrS2KRange, arrT2000r1, arrT2000r2, arrT2000r3, arrT2000r4, arrT2003r1, arrT2003r2]

// Function to handle dynamically altering the contents of the City List box
function handleChange(newDisplay)
{   var StateSelect, CitySelect, NumEntries, i
    StateSelect = document.frm.productNumber
    CitySelect = document.frm.modelNumber

  // Delete all entries in the cities list box
  for (i = CitySelect.length; i > 0; i--)
    CitySelect.options[i-1] = null

  // Add comment option to City List box
  CitySelect.options[0] = new Option("-- Select Model --",0)

  // If state is selected add its cities to the City List box
  if (newDisplay >= 0)
    NumEntries = arrStates[newDisplay].length
    for (i = 1; i <= NumEntries; i++)
     CitySelect.options[i] = new Option((arrStates[newDisplay])[i-1],(arrStates[newDisplay])[i-1])
  CitySelect.selectedIndex = 0


I am trying to find a way to populate the arrays (at the top of the script) dynamically with values from my access database.

Each array would come from a different recordset, and i have no problem sorting these out. I just need to know how I can put a field from the recordset in the array, followed by a comma, followed by the next record, followed by a comma, follwed by etc etc untill there's no records left.

Any ideas would be greatly appreiciated.

Thanks in advance

Question by:PaulCampbell

Accepted Solution

bitter_chicken earned 75 total points
ID: 10900412
Hey Paul - try this:

(example given for populating arrSSRange = ["SS065", "SS100", "SS150", "SS200", "SS3000"])

'make sure to create a recordset or whatever here
arrSSRange = ["<% Do Until Recordset.EOF
Response.Write Recordset.Fields(1)
%>","<% Recordset.Movenext
Loop %>"]


Expert Comment

ID: 10900670
Hi Paul,

You could try something like this :

<script type="text/javascript">
var arrMG

' Server-Side ASP/VBScript code
Dim objConn, strSQL, objRS
' create a connection to the Access database
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.Mappath("filename.mdb")

strSQL = "SELECT field FROM table;"
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open strSQL, objConn
' if there is at least one record, write the first element in the JavaScript array
If NOT objRS.EOF Then
  Response.Write "arrMG = [""" & objRS("field") & """"
  ' if there are more records, loop through them and add them to the JavaScript array
  Do While Not objRS.EOF
    Response.Write " , """ & objRS("field") & """"
  ' close the JavaScript array and insert a carriage return
  Response.Write "]" & vbCrLF
End If
' clean up the objects
Set objRS = Nothing

' clean up the objects
Set objConn = Nothing


This should output the following to the browser :

<script type="text/javascript">
var arrMG
arrMG = ["MG05LED" , "MG07LCD", "MG10LCD"]

For each JavaScript array that you are pulling out of the database, you will need to create another strSQL and objRS object, and loop through them like the one above.

Hope that helps,
 - Splat


Expert Comment

ID: 10904895
Heres my contribution, this would need to be done for each RecordSet that you have...

If not rsList.EOF then
      Response.Write "<script language='JavaScript'>" & vbCRLF
      Response.Write "<!--" & vbCRLF
      Response.Write "arrMG = [" & Chr(34) & rsList("FieldName") & Chr(34)
      If not rsList.EOF then
            Do while not rsList.EOF
                  Response.Write "," & Chr(34) & rsList("FieldName") & Chr(34)
      End If
      Response.Write "]" & vbCRLF
      Response.Write "//-->" & vbCRLF
      Response.Write "</script>" & vbCRLF
End If

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
app server have enough resources... 2 52
The Best Website Creation Tools 14 73
PHP Form Calculate Total Price 10 75
Is sending authentication through SOAP secure? 3 13
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

791 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