Page 1 of 1 1
Topic Options
#175025 - 2007-03-28 02:53 PM Extracting data from Access Database
madboyo Offline
Fresh Scripter

Registered: 2007-03-28
Posts: 6
Loc: UK
Hi guys this is the first post on this site. I'm not a newbie regarding Kix. I use the tool to manage my users and workstations, reading registry, getting file versions, login scripts and use alot of the basic commands which have served me well up until now. Now I'm out of my comfort zone!

I have a requirement to get at information held in an Access 2000 database. The information I need is held in a table called "Person" and in a field called "Email Address". I need to check each email address entry the customers have input. The software has basically allowed a free form cell so some customers have put anything in this field. I need to check for invalid email addresses i.e. anything without a @ in it seems easiest way and disgard anything else. I also need to check for typos such as leading, trailing spaces or even a space in between. Once all that has been done then I need to output the valid addresses to a txt file with each entry delimited by a semi colon (;) or even in a simple list. I'm sure by now you have worked out why I want this data like this. Some day I'll get round to automating the mail creation script from this address file to mailshot our customers with offers.

I have searched this site and more. I've read alot of the articles written by Sealeopard regarding these functions and have done the basics which are hopefully correct. Not having any db or programming skills other than basic KiXtart leaves me at a distinct disadvantage but hey I'm here to learn.

The script below is the starting of my effort. I've created the connection, made a record set and hopefully populated it with the data from the area I identified earlier on. Each function @error code returns 0 so I'm assuming all of what I have done is correct. My problem is now where do I go from here? How do I get to the data? I can't output the recordset either to the screen or a file. I'm guessing I can either output the raw data to a file then read each line and build a new file with the end result or maybe there are commands that can be run directly on the recordset and then output the filtered data to a single txt file at the end?

 Code:
; ###############################################################################################
; 
; Kixstart Script - Client Mailing List
;
; Script purpose   : Extract email addresses from database
;                    Disgard duplicate entires 
;                    Disgard invalid email address (i.e. anything without an @ in it)
;                    Disgard spaces due to typos (beginning, middle and end of address)
;                    Output to a file each email address in a string delimited by a semicolon (;)
;
; Kix32 Version    : 4.53.0.0
;
; Script Date      : 27/03/07
; Script Version   : 1.0
;
; ###############################################################################################


; #### Set Variables ####

DIM $DSN
DIM $SQLQueryRS

$DSN = "DRIVER={Microsoft Access Driver (*.mdb)}; UID=; PWD=; DBQ=ActinicCatalog.mdb"
$SQLQueryRS = "SELECT 'Email Address' FROM 'Person'"



; #### Open Database Connection ####

$objConn = DBConnOpen("$DSN")
? "@ERROR"



; #### Open Database Recordset ####

$recordset = DBGetRecordset("$objConn, $SQLQueryRS")
? "@ERROR"



; #### Close Database Connection ####

$retcode = DBConnClose($objConn)
? "@ERROR"


Top
#175026 - 2007-03-28 02:55 PM Re: Extracting data from Access Database [Re: madboyo]
madboyo Offline
Fresh Scripter

Registered: 2007-03-28
Posts: 6
Loc: UK
I'm not asking for people to write the code for me but just to point me in the right direction and maybe some links to other scripts that do similar functions or use the commands I need to use.
Top
#175030 - 2007-03-28 04:12 PM Re: Extracting data from Access Database [Re: madboyo]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
A couple of things I noticed...

 Code:
$recordset = DBGetRecordset("$objConn, $SQLQueryRS")

The arguments you pass to DBGetRecordset shouldn't be in quotes. Just:

 Code:
$recordset = DBGetRecordset($objConn,$SQLQueryRS)

...is sufficient.

On a SQL note, if you want to ingore duplicate email addresses, just add DISTINCT into your SELECT clause:

 Code:
"SELECT DISTINCT 'Email Address' FROM 'Person'"

Top
#175041 - 2007-03-28 07:39 PM Re: Extracting data from Access Database [Re: madboyo]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Once you get to your data, you will need a method to validate the data. Speaking strictly from a character matching perspective, I would recommend that you use Regular Expressions to valid the entry as a valid email address format. RegEx functionality is available via COM.

URL of interest: RFC: Regular Expression UDFs
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#175088 - 2007-03-29 10:27 PM Re: Extracting data from Access Database [Re: eriqjaffe]
madboyo Offline
Fresh Scripter

Registered: 2007-03-28
Posts: 6
Loc: UK
 Originally Posted By: eriqjaffe
A couple of things I noticed...

 Code:
$recordset = DBGetRecordset("$objConn, $SQLQueryRS")

The arguments you pass to DBGetRecordset shouldn't be in quotes. Just:

 Code:
$recordset = DBGetRecordset($objConn,$SQLQueryRS)

...is sufficient.

On a SQL note, if you want to ingore duplicate email addresses, just add DISTINCT into your SELECT clause:

 Code:
"SELECT DISTINCT 'Email Address' FROM 'Person'"


Thankyou for your reply. The adding of DISTINCT into the SQLquery will save me alot of time finding and eliminating duplicates.

I did try your suggestion of removing the quotes and the script will error out with "ERROR : expected ')'!". Add the quotes back in and the script functions normally.

Top
#175089 - 2007-03-29 11:09 PM Re: Extracting data from Access Database [Re: madboyo]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
That's strange, because I use DBGetRecordset() in a handful of scripts, and none of them need the quotation marks.

The only other thing I see different is that I don't bother to DIM the $DSN and $SQLQueryRS variables, but that may not have anything to do with it.

Top
#175090 - 2007-03-29 11:21 PM Re: Extracting data from Access Database [Re: madboyo]
madboyo Offline
Fresh Scripter

Registered: 2007-03-28
Posts: 6
Loc: UK
I'm sorry I know this is very basic but how do it output the record set to a flat file once i'm done sorting/validating the data?
Top
#175091 - 2007-03-30 12:48 AM Re: Extracting data from Access Database [Re: Howard Bullock]
madboyo Offline
Fresh Scripter

Registered: 2007-03-28
Posts: 6
Loc: UK
 Originally Posted By: Howard Bullock
Once you get to your data, you will need a method to validate the data. Speaking strictly from a character matching perspective, I would recommend that you use Regular Expressions to valid the entry as a valid email address format. RegEx functionality is available via COM.

URL of interest: RFC: Regular Expression UDFs


Howard, thankyou for your input. I'm sure by now you have got an idea of my technical expertise, or lack of it, for the more advanced parts of Kix. I have a massive shortfall in knowledge at this level of scripting and honestly believe that I have no right to implement a script I have little or no understanding of. How could I possibly trust the data if I have little or no understanding of how I got it. With that in mind I just envisaged pulling the data out into a file made up of a line of each entry then work with commands such as "instr" etc to produce a final flat file of all entries with an "@" and without spaces and without duplicates. Very crude I know but until I can raise my knowledge base to the level required to utilise UDF's etc then that is all I have to work with. Just trying to be honest rather than being ungrateful ;\)

Top
#175101 - 2007-03-30 03:47 PM Re: Extracting data from Access Database [Re: madboyo]
eriqjaffe Offline
Hey THIS is FUN

Registered: 2004-06-24
Posts: 214
Loc: Arlington Heights, IL USA
You could pump the data to a CSV file with something like this...

 Code:
$ = redirectoutput(@SCRIPTDIR + "\foo.csv",1)

for $row=0 to ubound($recordset,1)
 for $column=0 to ubound($recordset,2) - 1
   $recordset[$row,$column] + ","
 next
 $recordset[$row,ubound($recordset,2)] + @CRLF
next

$ = redirectoutput("")

Top
#175135 - 2007-03-31 10:37 PM Re: Extracting data from Access Database [Re: eriqjaffe]
madboyo Offline
Fresh Scripter

Registered: 2007-03-28
Posts: 6
Loc: UK
And the saga continues

### Question for MODS ### - Not sure if you want this in a new topic given the code has completely changed? If so then this topic could be deleted.

The original code that I posted failed to connect to the database. After spending loads of time trying to work out why I decided to connect using a different method. Obviously all I have done is cut and paste from other peoples scripts and I can finally output a result to the screen.

The problem is that the value returned to the screen is the title of the column. It does return the same number of results to the screen as is shown when viewing the table "Person" / column "Email Address" in the MS Access GUI.

I have included the new code including all the error checking and minus closing connections and clearing down of the objects. I would appreciate any feedback on why the tile is being returned not the value in each of the rows.

 Code:

$= SetOption("WrapAtEol", "On")

; #### Set Variables ####

$DBPath = "@scriptdir\ActinicCatalog.mdb"
$CNstring = "provider=microsoft.jet.oledb.4.0;data source=$DBpath;persist security info=false"
$CMDtxt = 'SELECT "Email Address" FROM Person'



; #### Create ADO Objects ####

$cn = CreateObject ("ADODB.Connection")
? "ADO Conn       ERROR @ERROR - @SERROR"

$cmd = CreateObject ("ADODB.Command")
? "ADO Cmd        ERROR @ERROR - @SERROR"

$rs = CreateObject ("ADODB.RecordSet")
? "ADO RS         ERROR @ERROR - @SERROR"



; #### Open Database connection ####

$cn.connectionstring = $CNstring
? "ADO CN String  ERROR @ERROR - @SERROR"

$cn.Open
? "ADO CN Open    ERROR @ERROR - @SERROR"



; #### Set Active commands ####

$cmd.activeconnection = $cn
? "ADO ActConn    ERROR @ERROR - @SERROR"

;$rs.activecommand = $cmd       Is this needed? It errors if left in!
;? "ADO ActCmd     ERROR @ERROR - @SERROR"



; #### Open Recordset and Populate with results of Query ####

$cmd.commandtext = $CMDtxt $rs.Open ($cmd)
? "ADO Query      ERROR @ERROR - @SERROR"



; #### Move to the first record ####

$rs.Movefirst()
? "ADO Movefirst  ERROR @ERROR - @SERROR"
? ""



; #### Read all data in the recordset ####

$L=0
$Field = $rs.Fields.count -1
do
for $i=0 to $Field ; count through each column
$rs.Fields.item($i).Value ; spit out the value in that column
if $i<>$Field ", " endif ; comma separate fields
$L = $L +1
? "Number of times rs.Fields.Item(i).Value has been called:" $L
next

$rs.MoveNext() ; goto the next record
? "ADO Movenext  ERROR @ERROR - @SERROR"
? ; CRLF separate records

until $rs.EOF ; until we reach the End Of the file



Edited by madboyo (2007-03-31 10:40 PM)

Top
#175136 - 2007-04-01 04:34 AM Re: Extracting data from Access Database [Re: madboyo]
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I would say the original code did not connect to the database because you did not provide the full path to the database. In your second script you're now providing the full path, "ActinicCatalog.mdb" versus "@scriptdir\ActinicCatalog.mdb".

You should give the DBCommand() UDF a try.
_________________________
There are two types of vessels, submarines and targets.

Top
#175137 - 2007-04-01 09:29 AM Re: Extracting data from Access Database [Re: madboyo]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Or maybe "Hey, Scripting Guy!" can help you? I presume following article is usefull and can be converted to KiX.
Hey, Scripting Guy! How Can I Use Windows PowerShell to Pull Records From a Microsoft Access Database?

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 795 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.065 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org