473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Prompt user to select file (Access VBA)

Hi anyone!
I am working to translate an Excel macro into Access and I'm having little luck with a couple lines of code. I can't figure out how to prompt the user to select a file (not to past a path and filename) but to actually browse for a file :) Here is the Excel code I already have:

Dim TxtName As String
TxtName = Application.GetOpenFilename("TextFiles (*.txt), *.txt", , "Select The file you wish to update:")


I'm also hoping to strip off the first few blank lines from the text file I am importing. In excel it was easy because I could simply tell the program to look at the 4th line in the text file:
'Import the .txt file:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & CStr(TxtName), Destination:=Range("A1"))
.TextFileStartRow = 4

But my macro in Access doesn't seem to give that option. All I have in access so far is:
DoCmd.TransferText acImportDelim, "InventoryObsoSpec", NewTableName, filename, No

But that grabs a bunch of blank lines into my data table. Can anyone help me out there???
Thanks!
May 9 '07 #1
2 35781
Denburt
1,356 Expert 1GB
You will need to add a refference to MS Office, go into the VBA window - Tools -References


Expand|Select|Wrap|Line Numbers
  1. Function FindTxtFile(strFNme As String, strSearchPath As String) As String
  2. ' Displays the Open dialog box for the user to locate
  3. ' the BackEnd database. Returns the full path.
  4.  Dim myDialog As FileDialog
  5.     'Declare a variable as a FileDialogFilter object.
  6.  Dim vrtSelectedItem As Variant
  7.     'Set the FileDialogFilters collection variable to
  8.     'the FileDialogFilters collection of the SaveAs dialog box.
  9. Set myDialog = Application.FileDialog(msoFileDialogOpen)
  10. With myDialog
  11.     .AllowMultiSelect = False
  12.      .Filters.Add "Text", "*.txt", 1
  13.     .Title = "Where Is The Text File?"
  14. '    Debug.Print Right(strSearchPath, Len(strSearchPath) - 10)
  15.     .InitialFileName = strSearchPath & strFNme
  16.     If .Show = -1 Then
  17.         For Each vrtSelectedItem In .SelectedItems
  18.             FindTxtFile = Trim(vrtSelectedItem)
  19.         Next vrtSelectedItem
  20.     Else
  21.     End If
  22.    Set myDialog = Nothing
  23. End With
  24. End Function
  25.  
May 9 '07 #2
JConsulting
603 Expert 512MB
Hi anyone!
I am working to translate an Excel macro into Access and I'm having little luck with a couple lines of code. I can't figure out how to prompt the user to select a file (not to past a path and filename) but to actually browse for a file :) Here is the Excel code I already have:

Dim TxtName As String
TxtName = Application.GetOpenFilename("TextFiles (*.txt), *.txt", , "Select The file you wish to update:")


I'm also hoping to strip off the first few blank lines from the text file I am importing. In excel it was easy because I could simply tell the program to look at the 4th line in the text file:
'Import the .txt file:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & CStr(TxtName), Destination:=Range("A1"))
.TextFileStartRow = 4

But my macro in Access doesn't seem to give that option. All I have in access so far is:
DoCmd.TransferText acImportDelim, "InventoryObsoSpec", NewTableName, filename, No

But that grabs a bunch of blank lines into my data table. Can anyone help me out there???
Thanks!

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim TxtName As String
  3. dim strLine as string
  4. TxtName = Application.GetOpenFilename("TextFiles (*.txt), *.txt", , "Select The file you wish to update:")
  5. open txtname for input as #1
  6. for I = 1 to 5
  7. Line Input #1, strLine
  8. next I
  9. do until eof(1)  
  10. Line Input #1, strLine '<--now you're on record 5...what do you want to do with it?
  11. 'do your stuff here
  12. loop
  13. close #1
  14.  
  15.  
May 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: andreas | last post by:
Hi there, I have the following situation: 1. DOT.NET Application is started 2. DOT.NET Application instantiates Access.Application.8 3. Opens a specified database (MDB) 4. DOT.NET...
3
by: Big Time | last post by:
I'm looking into buying a book to learn Access VBA programming. I am very familiar with Access however I am new to programming Access with VBA. Are there any recommendations for books for...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: hedgracer | last post by:
I have a strange problem with a docmd.transfertext statement. I have set up an import specification to transfer in a .txt file by fixed width. When I do the import using the specification the file...
8
by: Darryl Kerkeslager | last post by:
Currently I am using the RegExp object to parse a large dataset in an Access table - but this table was exported from SQL Server, and the very correct question was asked - why not just do it in SQL...
4
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from...
76
by: JFKJr | last post by:
Hi, the following Excel VBA code is used to select 5 rows by double clicking the cell in the first column corresponding to that row. The code is working fine in excel. But, I need Access VBA code,...
26
by: JFKJr | last post by:
Hello everyone, I have added a ComboBox in excel spreadsheet using the following Access VBA code. Public eXL As New eventsXL Function Create_ComboBox() With eXL If .XL Is Nothing Then Set...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.