Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a msaccess database?

Status
Not open for further replies.

Phailak

Programmer
Apr 10, 2001
142
CA
Hail,

I just started to try to use vbscript since I read it was easy to use if yuo know VB. Well, it does seem relatively easy, but I am stuck on something. Let's say I want a name to appear in a text box after pressing a button. I would randomly choose that name from a msaccess database. Now in VB this would be easy, but I don't know how to retrieve the data using vbscript.

Let's say path to mdb is c:\My documents\Names.mdb
Table name is RandomNames
How would I set database and Recordset or is it totally different from VB?

Phailak
 
It's almost exactly the same as it is in VB --

Where you might say this in VB:
dim con as new ADODB.Connection

and

dim rs as new ADODB.Recordset


You would say this:
dim con, rs
set con = server.createObject ("ADODB.Connection")
set rs = server.createObject ("ADODB.Recordset")


If you can select a random number in VB, and then move to that record in your ADO recordset, then I'll spare you the boring details, because you can do it the same way you do in VB with VBScript --

just don't ever try to declare something as a certain type -- all variables are variant type in VBScript, and when you set objects, don't use the AS keyword -- do it as I demonstrated above.

Other than that, just remember for their super-duper-handy-dandy VBScript reference guide, and you should be well on your way to doing whatever you want to do with VBScript.

good luck! :)
Paul Prewett
 
Hail Paul,

Thanx for the code, but I can't make it work???

When I test it, it gives me the following error message, object required "server"?
Do I maybe have to reference something like Database objects in VB? I'm sure it's something really stupid that I'm missing but like I said, I'm just trying this out!

Phailak
 
Ok here goes...

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Gladiators</title>
</head>

<body bgcolor=&quot;#808080&quot; alink=&quot;#FFFF00&quot; title=&quot;Gladiators&quot;>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div align=&quot;center&quot;><font color=&quot;#000000&quot;><strong><em><font size=&quot;7&quot;>Gladiateurs</font></em></strong></font></div>
<br><br><br><br><br>
<div align=&quot;center&quot;><font color=&quot;#000000&quot;><strong><em><font size=&quot;6&quot;>UserID: </font></em></strong></font></div>
<div align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;txtUser&quot; maxlength=&quot;15&quot;></div>
<div align=&quot;center&quot;><font color=&quot;#000000&quot;><strong><em><font size=&quot;6&quot;>Password: </font></em></strong></font></div>
<div align=&quot;center&quot;><input type=&quot;password&quot; name=&quot;txtPassword&quot; maxlength=&quot;15&quot;></div>
<br><br>
<FORM>
<div align=&quot;center&quot;><INPUT TYPE=&quot;BUTTON&quot; VALUE=&quot;Login&quot; NAME=&quot;cmdLogin&quot;></div>
</FORM>
<script language=&quot;VBScript&quot;>
<!-- These comment delimiters ensure the code is hidden from those browsers that do not support Visual Basic Script
Sub cmdLogin_OnClick
if txtuser.value = &quot;Pat&quot; then
dim con, rs
set con = server.createObject (&quot;ADODB.Connection&quot;)
set rs = server.createObject (&quot;ADODB.Recordset&quot;)
msgbox &quot;Correct&quot;
ELSE
MSGBOX &quot;Incorrect&quot;
end if
End Sub
-->


</script>

</body>
</html>


I'm using Homesite if that helps...

Phailak
 
Ah -- I see --

server is (strangely enough) a server side object --

You are attempting to declare your objects in client side code...

My true suggestion would be to buy a book on ASP to learn all the ins and outs of it, but here's a quick overview -- although I'm not sure how much it will help --

First, you have to start the page by declaring a default server script language like this:
<%@language=vbscript%>

Then, to write the server side code, you have to enclose it between these tags:
<%
'put code here
%>

Server side code will be processed before any of your client side code...

If you put this code in the top of the page, it would create the objects:

<%
dim con, rs
set con = server.createObject (&quot;ADODB.Connection&quot;)
set rs = server.createObject (&quot;ADODB.Recordset&quot;)
%>

Another problem that I forsee in your immediate future is the fact that ASP is going to have to run in a server environment such as NT or PWS (Win9x) --

There are some good FAQ's floating around this site in this forum and in the ASP forum, and lots of good resources on the web such as:

to name only a few.

I stick to what I said up top about if you know VB, then moving to ASP really will be a breeze. There are some fundamental concepts about the technology, though, that you will need to get a hold of before you start trying to use it.

If you have some more specific questions, don't hesitate to post either here or over the the ASP forum (more people read that one) --

and good luck! :)
Paul Prewett
 
Thanx Paul, I'll look into those links, but I don't quite get where I need to put the code you gave me?

---> <%@language=vbscript%>, where exactly does this go, before the script declaration or what (referring to previous post where even html code is) and why do I need this when I declare vbscript here ---> <script language=&quot;VBScript&quot;>

Then does ---> <%
dim con, rs
set con = server.createObject (&quot;ADODB.Connection&quot;)
set rs = server.createObject (&quot;ADODB.Recordset&quot;)
%> < --- actually go within the script tags?

I know I'm asking a lot of questions, I guess meanwhile I'll go scan about like you suggested...

Phailak
 
<%@language=vbscript%>
goes before anything else on your page -- it should be the first line --

and this line:
<script language=&quot;VBScript&quot;>
tells the browser to interpret the code that's between that and the </script> tag, which is not what you want to ASP. ASP is a server technology, which means that the server has to interpret it, so that's why you use the

<%%>'s

and you don't put those between any <script> tags --

For completeness, I'll tell you that you can say:
<script language=vbscript RUNAT=server>

</script>

to get the same effect as <%%>'s, but it's not the convention -- mainly because it's longer...

So a page might look like this:
Code:
<%@language=vbscript%>
<html>
<head>
<title>Showing my ASP</title>
</head>
<%
dim con, rs
set con = server.createobject (&quot;ADODB.Connection&quot;)
set rs = server.createobject (&quot;ADODB.Recordset&quot;)
dim someVar
someVar = &quot;Hello, World!&quot;
%>
<body>
Blahblahblah -- here's some html stuff
<%
response.write(&quot;<p>Here's some ASP stuff: &quot; & someVar & &quot;</p>&quot;)
%>
</body>
</html>
Now, obviously, you haven't done anything with your connection and recordset object up there, but you can if you want to, and then use response.write's to output values to the screen wherever you want them... just enclose them in the <%%> script tags as I demonstrated.

The page output would look like this:

Blahblahblah -- here's some html stuff

Here's some ASP stuff: Hello, World!


:)
paul
 
Thanx again, Ok I know how to play with recordsets and stuff, but how do I tell it which table and which database to fetch, is it in the parameters, basically, what would the equivalent of the following be?

Dim db as database, rst as recordset
Set db = opendatabase(&quot;c:\my documents\gladiats.mdb&quot;)
set rst = db.openrecordset(&quot;Gladiators&quot;, dbopendynaset)
rst.findfirst &quot;Name = 'Phailak' &quot;
X = rst!Name

I mean I understand that I established a connection but where do I specify to what i connect to? If it's not too ch trouble of course...

Once again thanx for the help!

Phailak
 
Well, that is DAO, and I'm not qualified to give you advice on how to use that. I use ADO...

I would do the above with:

dim con, rs, strConn, sqlStatement
set con = server.createobject(&quot;ADODB.Connection&quot;)
strconn=&quot;PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=&quot;
strconn=strconn & server.mappath(..\my documents\gladiats.mdb) & &quot;;&quot;
con.open

set rs = server.createobject(&quot;ADODB.Recordset&quot;)
rs.activeConnection = con
rs.cursorType = 2 'adOpenDynamic
sqlStatement = &quot;SELECT * FROM yourTable WHERE name=&quot;&quot;Phailak&quot;&quot;&quot;

rs.open sqlStatement

dim x
x = rs(&quot;fieldName&quot;)


and now x would hold the value of the fieldName at whatever row your recordset is on...

 
Great, thanx a million, that's all I need, I guess I'm used to using DAO but your thing is just as good.

Phailak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top