HFX Forum

Programming => Visual Basic => Topic started by: geoinscho on March 04, 2005, 04:02:03 PM

Title: Need help connecting database to form controls
Post by: geoinscho on March 04, 2005, 04:02:03 PM
I'm using Visual Basic 6.0 at work (AAARGH!).  If I were using 7.0, this would be no problem.  But with 6.0 I can connect to the database...I even get a confirmation of connection after clicking on the "Test Connection" button.  

But I cannot get any of the controls on the form to pick up any data from the database.  When I click on the text box's DataSource attribute, it only gives me DataConnection1 as an option, but I can find NO other reference to DataConnection1 in the project.

And HELP doesn't work, because I get an error message that says that the MSDN collection is not available and must be reinstalled.  But I have no idea who has the MSDN software in order to reinstall it.

At first I thought MSDN meant (M)icro(S)oft (D)umb@$$ (N)etwork....but now I know it for sure.
________________________________________

It is a MicroSoft Access 2000 Database.
Thanks,
Geo
Title: Re:Need help connecting database to form controls
Post by: Cobra on March 06, 2005, 05:47:19 PM
Can ya give me more info..

What is the database, is it a Windows APP or is it a Web APP..

etc.. etc..

Title: Re:Need help connecting database to form controls
Post by: geoinscho on March 14, 2005, 08:43:25 AM
I am using Access 2000 on a Windows 98 Dell Computer.  I'm not sure what other information to look for.
Title: Re:Need help connecting database to form controls
Post by: Cobra on March 14, 2005, 12:12:37 PM
To access info from an Access database you should have something like

<%
dim cmd, conn, rs, strSQL
set conn=server.CreateObject("ADODB.Connection")
set rs=server.CreateObject("ADODB.Recordset")
set cmd=server.CreateObject("ADODB.Command")
filepath=Server.MapPath("database_path/here/database_name.mdb")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath

strSQL = "SELECT * TOP 1 FROM database_name"
rs.open strSQL, conn, 1, 1

%>
<form>
<input type="text" name="field_name" value="<%=rs("database_field")%>
</form>


--------------------------------------------------------

That example will connect to your database and pull whatever data is in "database_field" and place it as the default value in your form field.

if you want to read from that form then just use

request.form("field_name")

That what yer lookin for?
Title: Re:Need help connecting database to form controls
Post by: geoinscho on March 16, 2005, 08:08:43 AM
Sort of, except that I don't know where to add this block of code.  In school, it was as drag-and-drop; so, I think the course left me rather crippled when it comes to older versions of visual basic.  (Come to think of it, I probably used Visual Studio .NET.)

Could you please tell me where I would place the code?
Title: Re:Need help connecting database to form controls
Post by: Cobra on March 16, 2005, 09:21:49 AM
Sure man,
Normally i put my connection strings in an include file below the root level, but for this example we can leave it in the main file.

---------- dbconn.asp ----------
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<body>
<%
dim cmd, conn, rs, strSQL
set conn=server.CreateObject("ADODB.Connection")
set rs=server.CreateObject("ADODB.Recordset")
set cmd=server.CreateObject("ADODB.Command")
filepath=Server.MapPath("database_path/here/database_name.mdb")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath

strSQL = "SELECT * TOP 1 FROM database_name"
rs.open strSQL, conn, 1, 1

%>
<form>
<input type="text" name="field_name" value="<%=rs("database_field")%>
</form>

<%
rs.close
set strSQL = Nothing
%>
</body>
</html>
-------------------------------------------

Now just save that file with a ASP extension and it will connect to your database fine and display the top record from the database into your form.
Title: Re:Need help connecting database to form controls
Post by: geoinscho on April 01, 2005, 05:07:04 PM
Okay, but I'm still not clear on where to put it.
We never worked with ASP files or root files.
Title: Re:Need help connecting database to form controls
Post by: Cobra on April 04, 2005, 12:53:16 PM
Is this getting used on a website?

or are you writing a local application?

Because what i have given is for a website.
Title: Re:Need help connecting database to form controls
Post by: geoinscho on April 06, 2005, 10:49:12 AM
It's for a local windows-based application.