Solved

VBScript Variable Undefined error message

Posted on 2014-09-22
32
185 Views
Last Modified: 2014-10-27
I want the user to be able to login and add or delete their state information.  The delete portion works, but the add portion takes the state abbreviation (ex. AL) and puts a corresponding 1 in the SQL Server 2005 database.  I can't seem to get it to use the 2 char state abbreviation instead of the array counter.  If a 1=Alabama, a 2=Alaska, a 3=Arizona, etc.  

I don't want a number, I want the state abbreviation.  I know it has something to do with using a counter in the array with the state abbreviation field (vState_ID, defined as a char datatype).  I am trying to modify someone else's existing code that was originally designed using an int datatype.  I'm not very familiar with vbscript and don't fully understand what the code is doing.

I thought maybe a Cint conversion of the array might help, but it only generated more errors.

Any help would be greatly appreciated.  I've been beating my head against the wall for weeks on this.  This is the code:
<%@ Language=VbScript%>
<% Option Explicit %>
<% Response.Buffer = true %>
<!-- #include file=CommDecl.asp -->
<%
on error resume next
dim vPhy_LName            
dim VPhy_FName            
dim iCtr
dim iCtrd
dim iStateCounter
dim vDate
dim vState_ID()
dim vStateDesc()
dim v_choice
dim iAllAP
dim vStateFileAbbr()
dim vStateFileDesc()

vLicNo = Trim(Request("txtLicNo"))
%><!-- #include file=CheckAccess.asp --><%

Set vConn = server.CreateObject("ADODB.Connection")
Set vRs = server.CreateObject("ADODB.Recordset")
vConn.Open vDSN, vUID, vPswd
%><!-- #include file=ifError.asp --><%

vSQL = "GetPPbyLicNo 'Physician_Profile', '" & vLicNo & "' "
vRS.Open vSQL, vConn
%><!-- #include file=ifError.asp --><%
if not vRS.EOF then 
	vPhy_LName = trim(vRS("Phy_LName"))
	VPhy_FName = trim(vRS("Phy_FName"))
end if
vRS.close
%><!-- #include file=ifError.asp --><%

vSQL = "GetPPbyLicNo 'Phy_State', '" & vLicNo & "' "
vRS.Open vSQL, vConn
%><!-- #include file=ifError.asp --><%
iStateCounter = 0
do until vrs.EOF
	iStateCounter = iStateCounter + 1 
	Redim preserve vStateDesc(iStateCounter)
	Redim Preserve vState_ID(iStateCounter)
	vState_ID(iStateCounter) = trim(vRS("State_ID"))
	vStateDesc(iStateCounter) = trim(vRS("State_desc"))
	vRS.MoveNext		
loop
vRS.Close
%><!-- #include file=ifError.asp --><%

vSQL = "GetAll 'State_Codes' "
vRS.Open vSQL, vConn
%><!-- #include file=ifError.asp --><%

iAllAP = 0
do until vrs.EOF
	iAllAP = iAllAP + 1 
	redim Preserve vStateFileAbbr(iAllAP)
	redim Preserve vStateFileDesc(iAllAP)
	vStateFileAbbr(iAllAP) = trim(vRS("State_ID"))
	vStateFileDesc(iAllAP) = trim(vRS("State_desc"))
	vRS.MoveNext		
loop
vRS.Close
%><!-- #include file=ifError.asp --><%

Set vRS = nothing
Set vConn = nothing
%>
<html>
<link href="main.css" rel="stylesheet" type="text/css">

<body bgcolor="#FFFFFF" link="#002DC4" vlink="#BFA32F" alink="#006600">
<form Name="frmEdit">
<input Type="hidden" name="txtLicNo" value="<%=vLicNo%>">
<input Type="hidden" name="txtAction" value="<%=Trim(Request("txtAction"))%>">

  <table width="620" border="0" cellpadding="0" cellspacing="0">
    <tr>
	  <td height="40" valign="middle" background="images/header.gif">
	<div align="center"><font color="#FFFFFF" face="Georgia, Times New Roman, Times, serif">
	<!-- #include file="header.asp" -->
    </font></div></td>
	</tr>
  <tr align="center"> 
	  <td bgcolor="#E3D6A1" class="LargeFont"> 
        <input type="hidden" name="txtLName" value="<%=vPhy_LName%>">
		<%=vLicNo%>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<%=vPhy_LName%>,
		<input type="hidden" name="txtFName" value="<%=vPhy_FName%>">
		&nbsp;
		<%=vPhy_FName%>
	</td></tr>
</table>

<% Response.Flush() %>

  <table width="620" border="0" cellpadding="2" cellspacing="2">
    <tr align="left"> 
      <td height="20" colspan="2"></td>
    </tr>
    <tr align="left" class="subhead"> 
      <td width="19" bgcolor="#E3D6A1"></td>
      <td width="460" bgcolor="#E3D6A1"><b>Medical Licenses Held in Other States</b></td>
    </tr>
    <tr Valign="top" class="bodytext"> 
      <td width="19" Valign="top" bgcolor="#F8E8A2">&#149; </td>
      <td width="460" bgcolor="#F8E8A2"> 
      Please select States from the list<BR>
		<select Name="txtStatesLicensed">
          <option value="0"></option>
          <% for iCtrd = 1 to UBound(vStateFileAbbr)%>
          <option <%if v_choice = vStateFileDesc(iCtrd) then Response.Write(" selected ") %> value="<%=vStateFileAbbr(iCtrd)%>"><%=vStateFileDesc(iCtrd)%></option>
          <% Next %>
        </select>
		<a Href="javascript:jfAdd()"><img alt="Add" border="0" src="images/btn_save.gif" width="33" height="18"></a></td>
    </tr>
    <tr Valign="top"> 
      <td height="20" colspan="2" Valign="top">&nbsp;</td>
    </tr>
  </table>

  <input type="hidden" name="txtNoofObjects" value="<%=iStateCounter%>">
    <br>
  <% if iStateCounter > 0 then %>
	<table width="620" border="0" cellpadding="2" cellspacing="2">
		<tr>
			<td colspan="2" bgcolor="#E3D6A1" class="subhead">Select to delete</td>
		</tr>
	<% for iCtr = 1 to iStateCounter %>
		<tr bgcolor="#F8E8A2" class="bodytext"> 
			<td width="45"><input type="checkbox" name="chkPGTP"></td>
			<td width="561"><input type="hidden" size="1" name="txtStatesLicensed" value="<%=vState_ID(iCtr)%>">
			  <%=vStateDesc(iCtr)%>
		  </td>
	  </tr>
	<% Next %>
	</table>		
	<%end if %>
	<a Href="javascript:jfBack()"><img alt="Back to Practitioner Profile" border="0" src="images/btn_Back2.gif"></a>
  <% if iStateCounter > 0 then %>
	<a Href="javascript:jfDelete()"><img alt="Delete" border="0" src="images/btn_Delete.gif" WIDTH="41" HEIGHT="18"></a>
	<% end if %>

<br>
</form>
</body>

Open in new window




Thanks!
0
Comment
Question by:ctriplett
  • 15
  • 13
  • 4
32 Comments
 
LVL 32

Expert Comment

by:Big Monty
ID: 40336866
whats the exact error you're getting? on what line number?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40336884
You are creating 2 one dimensional arrays and you should be creating 1 two dimensional array.  However, this would be much easier if you used a dictionary.

<%
Dim dStates
Set dStates=Server.CreateObject("Scripting.Dictionary")

do until vrs.EOF
   dStates.Add vRS("State_ID"),vRS("State_desc"))
vrs.movenext
loop
%>

Open in new window

Then to use
<%
Response.Write("The value of key '1' is: " & dStates.Item("1"))&"<br>"
Response.Write("The value of key 1 is: " & dStates.Item(1))
%>

Open in new window


http://www.w3schools.com/asp/asp_ref_dictionary.asp
http://support.microsoft.com/kb/246067
0
 

Author Comment

by:ctriplett
ID: 40336896
It's a terrible, generic "Variable is undefined 09/22/2014 11:56:16 am".

No line number is given.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40336899
looking at this code, it seems like you're filling up different arrays from the recordset:

do until vrs.EOF
      iStateCounter = iStateCounter + 1 
      Redim preserve vStateDesc(iStateCounter)
      Redim Preserve vState_ID(iStateCounter)
      vState_ID(iStateCounter) = trim(vRS("State_ID"))
      vStateDesc(iStateCounter) = trim(vRS("State_desc"))
      vRS.MoveNext            
loop

Open in new window


I believe your FOR loop is causing the issue, because arrays are 0-based indexed, and it looks like you didn't account for that. Just change your code to:

      Please select States from the list<BR>
            <select Name="txtStatesLicensed">
          <option value="0"></option>
          <% for iCtrd = 0 to UBound(vStateFileAbbr) - 1%>
          <option <%if v_choice = vStateFileDesc(iCtrd) then Response.Write(" selected ") %> value="<%=vStateFileAbbr(iCtrd)%>"><%=vStateFileDesc(iCtrd)%></option>
          <% Next %>
        </select>
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40336908
same with this code as well:

<% if iStateCounter >= 0 then %>
      <table width="620" border="0" cellpadding="2" cellspacing="2">
            <tr>
                  <td colspan="2" bgcolor="#E3D6A1" class="subhead">Select to delete</td>
            </tr>
      <% for iCtr = 0 to iStateCounter - 1 %>
            <tr bgcolor="#F8E8A2" class="bodytext">
                  <td width="45"><input type="checkbox" name="chkPGTP"></td>
                  <td width="561"><input type="hidden" size="1" name="txtStatesLicensed" value="<%=vState_ID(iCtr)%>">
                    <%=vStateDesc(iCtr)%>
              </td>
        </tr>
      <% Next %>
      </table>            
      <%end if %>
0
 

Author Comment

by:ctriplett
ID: 40336914
Thanks for the quick reply, Scott.  If I were to use your code, how do I make it work with my code?  The javascript function that I use to add the state information looks like this:

function jfAdd(){
      var vForm = document.frmEdit;
      var submitFlag = true;
      var vMsg = "Following fields are required: ";
      
      if (document.frmEdit.txtNoofObjects.value < 1){
            if (vForm.txtStatesLicensed.selectedIndex <= 0) {
                  vMsg = vMsg + "\n - Select the State " ;
                  if (submitFlag == true){vForm.txtStatesLicensed.focus()}
                  submitFlag = false;
            }
      }else{
            if (vForm.txtStatesLicensed[0].selectedIndex <= 0) {
                  vMsg = vMsg + "\n - Select the State " ;
                  if (submitFlag == true){vForm.txtStatesLicensed[0].focus()}
                  submitFlag = false;
            }
      }
      if (submitFlag == false) {
            alert(vMsg);
      }else{
            document.frmEdit.action = "Save5.asp";
            document.frmEdit.method = "post";
            document.frmEdit.submit();
      }


Would I be replacing this function with your code?

Thanks
0
 

Author Comment

by:ctriplett
ID: 40336932
I tried your changes, Big Monty.  Thank you for the quick response.  I'm still receiving "Variable is undefined" message.  Could it have something to do with the javascript function that is called?

function jfAdd(){
      var vForm = document.frmEdit;
      var submitFlag = true;
      var vMsg = "Following fields are required: ";
      
      if (document.frmEdit.txtNoofObjects.value < 1){
            if (vForm.txtStatesLicensed.selectedIndex <= 0) {
                  vMsg = vMsg + "\n - Select the State " ;
                  if (submitFlag == true){vForm.txtStatesLicensed.focus()}
                  submitFlag = false;
            }
      }else{
            if (vForm.txtStatesLicensed[0].selectedIndex <= 0) {
                  vMsg = vMsg + "\n - Select the State " ;
                  if (submitFlag == true){vForm.txtStatesLicensed[0].focus()}
                  submitFlag = false;
            }
      }
      if (submitFlag == false) {
            alert(vMsg);
      }else{
            document.frmEdit.action = "Save5.asp";
            document.frmEdit.method = "post";
            document.frmEdit.submit();
      }
}
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40336939
my guess is that you have a variable (not necessarily related to the states data) that has a typo in it. Double check all of your variable names and make sure they are declared properly.

That's all client side code you have there, the Dictionary object is a server side object you would need to implement if you wanted to start from scratch. I recommend trying out the changes I made to the code before tearing everything down. Also keep in mind that dictionary objects are not as efficient as arrays unless you have large amounts of data. Since you're only listing out states, I don't think it's the most efficient way to go.

in fact, looking at this more closely, it looks like you're calling two different store procedures. why not just create one stored procedure that calls the other 2 and dump everything into one array?
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40336952
i couldnt find any declaration for these two variables, are they in an include file?

Set vConn = server.CreateObject("ADODB.Connection")
Set vRs = server.CreateObject("ADODB.Recordset")
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40336956
same with this line:

vSQL = "GetPPbyLicNo 'Physician_Profile', '" & vLicNo & "' "
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40337012
> If I were to use your code, how do I make it work with my code?

Just replace

do until vrs.EOF
      iAllAP = iAllAP + 1 
      redim Preserve vStateFileAbbr(iAllAP)
      redim Preserve vStateFileDesc(iAllAP)
      vStateFileAbbr(iAllAP) = trim(vRS("State_ID"))
      vStateFileDesc(iAllAP) = trim(vRS("State_desc"))
      vRS.MoveNext            
loop
vRS.Close

Open in new window

fyi,  only one of these lines will work below.  I think it should be dStates.Item("1")) (with the quotes).  Try it both ways but one at a time.  
Response.Write("The value of key '1' is: " & dStates.Item("1"))&"<br>"
Response.Write("The value of key 1 is: " & dStates.Item(1))

>The javascript function that I use to add the state information looks like this:
Let's tackle one thing at a time.    Get this to work as you need.

>Also keep in mind that dictionary objects are not as efficient as arrays unless you have large amounts of data.

Big monty is wrong and does not know what he is talking about.  You are trying to create a way to look up a key and output it's associated pair.  That is what a dictionary object does.  I wouldn't use it for 1000 rows of data, but for 50 or 100 it works nicely.  In general classic asp does a bad job working with arrays.
0
 

Author Comment

by:ctriplett
ID: 40337025
Yes, those lines of code are in an include file at the beginning of the script file, Big Monty.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40337431
sure scott - whatever you say...forgot you don't like when people disagree with you... Using GetRows() is a lot faster than populating a dictionary object by looping through a recordset. Plus, the data being discussed here is small, so it's a moot point anyways.

You are trying to create a way to look up a key and output it's associated pair.

I understood the question to be you're getting a "variable undefined" error message while trying to populate some drop down boxes from your database and nothing to do with looking up particular variables. is that correct?
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40337508
I couldn't find a declaration to vLicNo, is that in another file?
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40337591
BM, you are confusing the issues.    Looping through the db vs doing a look up using an array vs dictionary.

ctriplett, you can just as easily look up the database.  If you are doing a look up multiple times on the same page, then the dictionary or 2 dimensional array is the way to go.

You can also create a static include file that looks like

dim arrayStates(49)
arraySates(0) = "Delaware"
arraySates(1) = "Pennsylvania"
':
':
arraySates(49) = "Hawaii"

<%
x = 48
response.write "The selected State is "&arrayStates(x)
%>

Open in new window

The problem with this is if you needed to do any sorting it will not be as efficient.  

You can also create an ado recordset without the use of a database  http://msdn.microsoft.com/en-us/library/aa260348(v=vs.60).aspx

There are a lot of options.

For something small like this, I just find the dictionary quick and easy.  If I knew I only needed to look up the db for this once on a page, I would probably just use the db though.  The actual way would depend on how many calls to the db and the amount of traffic I expect.
0
 

Author Comment

by:ctriplett
ID: 40337596
Yes, Big Monty, the variable vLicNo is defined in the includes file.
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 32

Expert Comment

by:Big Monty
ID: 40337602
try commenting out the line that says

on error resume next

this may give more info on where the error is occurring
0
 

Author Comment

by:ctriplett
ID: 40338964
Big Monty,

Good morning.  This is the error I receive after commenting out the "On error resume next":

ADODB.Recordset error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.

/bpqapp/Edit5.asp, line 46

Any ideas?
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40339399
Good Morning :)

looks like this line is causing the issue:

vState_ID(iStateCounter) = trim(vRS("State_ID"))

does the column State_ID exist in the query

vSQL = "GetPPbyLicNo 'Phy_State', '" & vLicNo & "' "
0
 

Author Comment

by:ctriplett
ID: 40339512
You're exactly right!  I changed a variable name in the vSQL query from State_ID to Medstate.  Now my page loads and I get farther into the program, but when it tries to save my state from the dropdown selection, I get an error message about a type mismatch when I try to select "Arkansas".

This is the error:
Microsoft VBScript runtime error '800a000d'

Type mismatch: '[string: "AR"]'

/bpqapp/Save5.asp, line 35

And this is the code for the save5.asp file starting from line 34-46.
for iCtr = LBound(vMedstate) to UBound(vMedstate)
      if replace(vMedstate(iCtr), ",", "") > 0 then
            for jCtr = LBound(vMedstate) to UBound(vMedstate)
                  if replace(vMedstate(jCtr), ",", "") > 0 then
                        if replace(vMedstate(jCtr), ",", "") = replace(vMedstate(iCtr), ",", "") _
                              and iCtr <> jCtr then
                              vDuplicate = true
                              exit for      
                        end if
                  end if
            next
      end if
Next

Again, I can only assume that it's trying to use an int datatype when it needs to use a char datatype.  But I don't know how to work with these counters and the Lbound and Ubound statements.

Any ideas now?

Thanks!!!
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40339571
I don't think you can use the replace function like that, try this instead:

dim val

for iCtr = LBound(vMedstate) to UBound(vMedstate)
      val = vMedstate( iCtr )
      if val <> "" and isNumeric( val ) then
            for jCtr = LBound(vMedstate) to UBound(vMedstate)
                  if replace(vMedstate(jCtr), ",", "") > 0 then
                        if val = replace(vMedstate(iCtr), ",", "") and iCtr <> jCtr then
                              vDuplicate = true
                              exit for      
                        end if
                  end if
            next
      end if
Next

Open in new window

0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40339634
Some of the code is redundant.    When you are looping using
 for iCtr = LBound(vMedstate) to UBound(vMedstate)
val = vMedstate( iCtr )

Open in new window

there is no need to check if val is numeric or includes spaces.  It will be an integer.  You only need to check that vMedstate is a valid array.  Val is going to be some number between zero and however many rows of data minus one.

Are you trying to set the selected value of a drop down?
0
 

Author Comment

by:ctriplett
ID: 40342632
I finally got a moment to test out your code, Big Monty.  I get a lot further, but I'm getting the same error message about a type mismatch in another part of the code.  This code is very similar to what you modified earlier, but I can't tell what's it's doing.  So clueless.  The type mismatch error is on Line 77
9-24-2014-3-46-45-PM.jpg
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40342646
i'm not sure what it's doing either as it's using the replace function incorrectly :) change line 77 to:

if vMedstate(iCtr) <> "" then

I THINK it's supposed to be a check to make sure the value is not empty, so that's what I changed the code to.
0
 

Author Comment

by:ctriplett
ID: 40344397
Well, the good news is that it works!  Thanks, Big Monty.  But it's not actually adding multiple state selections from the drop down.  It will add one, save it, then if I select another, it will over write the first one.  Even if I select the same state over and over, I never receive the "Duplicate selection" error message.  I need to be able to add all 50 states, if I want.  And delete them all, too.

What the heck.  This stuff is making me crazy. It's so outdated and hard to follow.  I hate trying to figure out someone else's logic.

Again, any assistance would be most appreciated.  

Chris
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40344407
can you post the latest version of your code? I'll try to have a look this afternoon.
0
 

Author Comment

by:ctriplett
ID: 40344533
Sure thing.  I uploaded both complete files instead of sending you pieces.  I saved them as a text file so that I could safely upload.

Thanks again for looking at the code and trying to help me.

Chris
Edit5.txt
Save5.txt
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 40344696
in your code, your SELECT box is designed to be a single select box, you'll need to change it to:

<select Name="txtStatesLicensed" multiple>

This'll allow you to hold down the CTRL or SHIFT key to select multiple items in it.

On the server side, you'll need to add in code to save each option. I recommend using  FOR-EACH loop, something like:

<%
dim stateOpts, arrStateOpts
stateOpts = Request.Form("txtStatesLicensed")

if InStr( stateOpts, "," ) > 0 then   '-- if multiple items are selected, they will be posted in a comma delimited string
    arrStateOpts = Split( stateOpts, "," )
    for each stateOption in arrStateOpts
            if stateOption <> "" then
                 '-- build your sql and insert into your database. The value that was selected will be stored in the stateOption variable
             end if
     next
else   '-- only one option was selected, the value will be stored in the variable stateOpts
     '-- save to db
end if
%>

Open in new window


hopefully this gets you going in the right direction
0
 

Author Comment

by:ctriplett
ID: 40345022
I don't understand what this code is replacing.  This looks like its creating even more variables, which is confusing me even more.

Where do I put this code?
0
 
LVL 32

Accepted Solution

by:
Big Monty earned 500 total points
ID: 40346877
the first thing you need to change is your select box and add the "multiple" keyword to it so it supports the ability to select more than one option at a time:

<select Name="txtStatesLicensed" multiple>

the second bit of code I gave you should replace the code you're using to save the values from txtStatesLicensed to the database. It's a bit hard following your save file, I'm not sure exactly what it is supposed to be doing, that's why I just gave this bit of code, hoping you could piece it into your existing code.
0
 

Author Comment

by:ctriplett
ID: 40376886
Thank you for your suggestions.  I'm still struggling with this during my "off hours", so forgive my lack of response.  I will be focusing on this again this week.
0
 

Author Closing Comment

by:ctriplett
ID: 40406995
Big Monty's suggestions were very helpful, but since I'm not able to figure out the logic in some of the code, I went ahead and revamped the code in .Net.

Things are working great now!

Thanks for all of your help.

Chris
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 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

12 Experts available now in Live!

Get 1:1 Help Now