Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2006, 02:15 AM
Authorized User
 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default MS Access Query

Hi,

I'm running some statistics compilation using Access. I'll need to import data from .csv format to Access before generating the reports. The problem is that the data may sometimes be not available for import. How can I include in Access a condition such that it will only execute the macro to import the .csv file only when the file is available?

Pls advise.

Many thanks.

 
Old October 17th, 2006, 02:11 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I don't have the script in front of me, but you can check for the existence of the file before you do anything using Scripting.FileSystemObject and then If objFile.FileExists()

I don't know the syntax off hand but could get it to you tomorrow.




mmcdonal
 
Old October 17th, 2006, 07:56 PM
Authorized User
 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, many thanks. look forward to receiving the script from you tomorrow.

 
Old October 18th, 2006, 06:50 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Okay, this will work:

Dim objFSO 'no variable type declaration or use Variant

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\Path\Filename.csv") Then

   ...do your processing here
Else
   MsgBox("File does not exist")
   Exit Sub
End If

This VBScript code should work within Access unless your security settings prevent scripts from working outside of the application or your system settings prevent scripts from scanning file systems. This is rare, however, since most people use default settings.

When the user opens Access, they will want to make sure they don't put the app in sandbox mode.

Anyway, I use this sort of scripting all the time in my apps.

Did this work for you?



mmcdonal
 
Old October 18th, 2006, 06:54 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You can also add this to the script to make it more manageable:

Dim sFile As String

sFile = "C:\path\filename.csv"

Then...

If objFSO.FileExists(sFile) Then
...

This will allow you to take the file name from various places as a variable instead of hard coding it.

HTH

mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Access Query neha2007 SQL Language 1 July 12th, 2007 10:11 AM
ms access query umeshtheone VB Databases Basics 1 June 1st, 2007 10:27 AM
MS Access Query rsm42 ASP.NET 1.0 and 1.1 Basics 2 March 20th, 2007 12:32 PM
I am trying to convert a MS Access Query to a SQL? WebLadyBug SQL Server ASP 3 March 9th, 2007 12:39 PM
Ms Access stored query problem turker Classic ASP Databases 0 December 27th, 2005 01:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.