Re: Command Line Query Tool for Access DB

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Gregg,

If you want to use a stored Access query that requires a parameter (e.g.

PARAMETERS [MinimumHeight] Single;
SELECT * FROM MyTable
WHERE Height >= [MinimumHeight]

) AFAIK you can't do it without using Access. But there's no problem if
your script can concatenate the parameter(s) into a SQL SELECT
statement. You can use OLE and DAO or ADODB as in my previous example,
but use the SQL to open a recordset which you iterate through and print
to STDOUT. Alternatively, and perhaps better, use ODBC. I haven't tried
that in VBScript, but here's an example in Perl:

#start of script
use strict;
use Win32::ODBC; #not sure how well this handles Unicode...

die "ODBC_Select.pl: extracts data from an MDB database to STDOUT.

Syntax:
perl ODBC_Select.pl mdbfile SQL [[SQL]...]

mdbfile: filespec of mdb database file
SQL: a SELECT statement
" unless $ARGV[1];

my $mdb = shift @ARGV; #database file to work on
my $Conn = new Win32::ODBC(
"Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdb;");
die "Couldn't establish connection to $mdb: " unless defined $Conn;

foreach my $SQL (@ARGV) {
if ($Conn->Sql($SQL)) {
warn "SQL '$SQL' failed.\n";
warn "Error: " . $Conn->Error() . "\n";
next
} else {
my $firstrow = 1;
while($Conn->FetchRow()) {
my %Data = $Conn->DataHash();
my $line = "";
if ($firstrow) { #print field names
$line .= "$_\t" foreach sort keys %Data;
chop $line;
print STDOUT "$line\n";
$line = '';
$firstrow = 0;
}
$line .= "$Data{$_}\t" foreach sort keys %Data;
chop $line;
print STDOUT "$line\n";
}
}
};
$Conn->Close;
#end of script


On Sat, 30 Dec 2006 15:25:00 -0800, Gregg Dotoli
<GreggDotoli@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I need to query the file and have standard output saved. Launching Access is
overkill and inefficient. I want to run the query with a parameter. I don't
understand how a question like mine is strange. You sound like one of those
windows people who get lost at the DOS prompt.



"Gregg Dotoli" wrote:

Does anyone know if MS Access comes with a tool for batch file access to an
MDB file? I have a simple 5 field MDB and want to run queries at the shell
level.
If not, what good tools are available?

Thanks,
Gregg Dotoli

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
.



Relevant Pages

  • Convert MDB to ADP problem
    ... problem of transfoming of my MDB database to ADP project. ... The SQL form of this append query is: ... I would like to have a similar situation on may SQL ...
    (microsoft.public.access.conversion)
  • Re: Sql mdb
    ... On paradox i could split the time and date on 2 fields ... Now my record stil hase time and date separetly ... I need to make an sql on date and time ... The date and time delimiter in an mdb database is # and the date must be mm-dd-yyyy and time is hh:mm:ss if the field in the database is a datetime. ...
    (borland.public.delphi.database.ado)
  • Re: Access 2003 Project connecting to SQL 2005
    ... SQL Server 2005 works 'pretty darn well' for Access 2003 Data Projects ... things like field captions; lookup fields, ... We were at the end of converting an mdb database to an Access 2003 Project ...
    (microsoft.public.access.adp.sqlserver)
  • Re: [COR] Re: How do I lock a file?
    ... It is something to keep in mind that for a mdb database you ... can not do the same things as for an other database like SQL or MSDE or ... Cor ...
    (microsoft.public.dotnet.languages.vb)