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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database value to variable

Status
Not open for further replies.

fatmbk

Technical User
Jul 11, 2002
29
0
0
GB
I am currently implemeting an ASP page that requires me to read data from 1 field in an MS access database and then put the value's that it gets into a variable. Any ideas on how to do this?

Cheers,

Fatmbk
 
Fatbml,

Are you simply trying to create a recordset? If so try this:

Code:
<!-- #include file="adovbs.inc" -->
<!-- #include file="connect.asp" -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <meta HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">
 </head>
<body>

<%
  ' open a recordset, select the field in database table
  strSQL="SELECT theFieldWanted FROM yourTable;"
  objRS.Open strSQL, objConn

  ' assign a variable (can be any name) to the record value
   theFieldWanted = objRS("theFieldWanted")

  ' Loop through recordset to get all field values if more
  ' than 1 and write the result to the screen

  If objRS.EOF Then
      Response.Write "Sorry, no data"

  Else

  Do While Not objRS.EOF
  Response.Write theFieldWanted & "<br>"

  objRS.MoveNext
  Loop

  End If

  objRS.Close
  Set objRS = Nothing

  objConn.Close
  Set objConn = Nothing

%>
</body>
</html>
 
that is in effect what i need to be able to do. Basically it is for an admin section for sending out a weekly email to a list of email address in a database. I need to pass all the email address from the database into objCDO.Bcc. My thoughts were to create a variable to store the email addresses and then set odjCDO.Bcc to this variable. Hope that makes sense.

Any ideas how to do it?

Fatmbk
 
An alternative method is to dynamically assign values to dynamically named variables. The variable names could be generated from the database's table field name(s). NOTE: This example only works if you SELECT only one record. Otherwise the values would be 'overwritten' during the loop.

Code:
<%
dim dbpath, dbfile, connstr, conn, rs, SQL, tmpstr

dbpath="_database/" 'dbpath="" is valid if db is in rootdir
dbfile="mydbfile.mdb" 'access filename

connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.Mappath(""&dbpath&dbfile&"")&";"

SQL="SELECT field1, field2, field3 FROM tablename WHERE field1='"& some_identifying_value &"'"

set conn = server.createobject("adodb.connection")
conn.open connstr
set rs = server.createobject("adodb.recordset")
rs.open SQL, conn, 3, 3
if not rs.eof then
  for each field in rs.fields
    tmpstr=field.name&"=rs.fields("""&field.name&""")"
    execute(tmpstr)
    'for testing purposes
    'tmpstr="response.write("""&field.name&"=""&field.name&""<br>"")"
    'execute(tmpstr)
  next
end if
rs.close
set rs = nothing
conn.close
set conn = nothing
%>

So if you had a table setup like this:

mytable
id firstname lastname
1 bob smith

And your SQL statement was this:

SQL="SELECT id, firstname, lastname FROM mytable WHERE id=1"

you would end up with:

id=1
firstname="bob"
lastname="smith"

As stated above, this isn't meant to be neccessarily the 'best' solution, it is just meant to give you some ideas.

I reccommend using arrays for one record or many records, but for beginners (It says TechnicalUser next to your alias) it's a difficult undertaking, especially if debugging is invloved (and it always is!). The connection and recordset methods are meant to be as generic as possible for easy conversion to your own project, that is if you find the code useful.


-a6m1n0

"Don't try to reinvent the wheel." -My HS FORTRAN Professor
 
Are you trying to create these variables while looping through "email" field and assigning them to a variable to insert into a CDONTS mail script? If so please post code so we can see exactly what you are trying to do. Thanks.

Brian
 
Yes - i'll try again at explaining it a bit better!

I have a database with a list of email addresses stored in it. I want to be able to send an email, using CDONTS, to all the email addresses stored within the database. I need to know how to get this list of email addresses into the objCDO.Bcc variable of CDONTS. My script works with single email addresses by just adding them using a text box input via a form. I'm just unsure how i get all the email addresses within the database into the BCC field.

Cheers,

Fatmbk
 
Code:
'[red]objConn[/red] is your connection object

strEmail = ""
strSQL = "SELECT email from myTable"
Set objRS = [red]objConn[/red].Execute(strSQL)

If NOT objRS.EOF Then
  While NOT objRS.EOF
    If strEmail = "" Then
      strEmail = objRS("email")
    Else
      strEmail = strEmail & ";" & objRS("email")
    End If
    
    objRS.MoveNext
  Wend
End If

Set objRS = Nothing

objCDO.Bcc = strEmail

Tony
_______________________________________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top