473,398 Members | 2,525 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,398 software developers and data experts.

Diificult to find answers to the easy questions

Hi everyone,

I am a newbie to VB and making great use of various web resources to find answers. However, the simple topics don't seem to be covered anywhere.

Here goes. I have some code that opens the windows open file dialog box and prompts the user to locate the file. The file string is then stored in the variable varFileName. So far so good. I then want to use the file name in a DoCmd.TransferText command to get the text file into my database.

How do I pass the variable (which is stored as K:etcetcetcetc/etc.csv) into the TransferText command?

Heres the code, thanks in advance, Stuart

Private Sub cmdImport3_Click()

Call OpenTextFile

DoCmd.TransferText acImportFixed, "Daily Rental Analysis Import Specification", _
"CM Daily Rental Report", "How do I get the bold bit below in here???"

MsgBox ("The file was imported")

End Sub



Public Function OpenTextFile(Optional varDirectory As Variant) As Variant
Dim strFilter As String, lngFlags As Long, varFileName As Variant

lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If

strFilter = ahtAddFilterItem(strFilter, "csv files (*.csv)", "*.csv")
strFilter = ahtAddFilterItem(strFilter, "Any files (*.*)", "*.*")
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:="Open a csv file ...")
If Not IsNull(varFileName) Then
varFileName = varFileName
End If
OpenTextFile = varFileName

End Function
Dec 2 '09 #1
4 1210
jeffstl
432 Expert 256MB
You need to make your varFileName variable be a Public Shared variable at the top of your module.

This will make it accessible across multiple functions in the same module. In other words, once you set it to a value....as long as the function you want to read that value is called AFTER it has been set...you can get it in there

Expand|Select|Wrap|Line Numbers
  1. 'at the very top somewhere
  2.  
  3. Public Shared varFileName as String
  4.  
  5. 'Run this only 
  6. Sub MainExample
  7.   SetFileName()
  8.   PrintFileName()
  9. End Sub
  10.  
  11.  
  12. Function PrintFileName
  13. 'stuff
  14. msgbox varFileName
  15. End Function
  16.  
  17. Public Function SetFileName
  18. 'stuff
  19. varFileName = "Something"
  20. End Funciton
  21.  
  22.  
Dec 2 '09 #2
Thanks Jeff,

I tried to declare the varaible at module level as Public Shared, however, VB just changes it to Public (module code below). The variable holds the correct value until it steps out of the module and back into the function that called it. I'm using Access 2000 unfortunately, would this be causing the issue?

Thanks for your help.

Public Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant

Dim strFilter As String
Dim lngFlags As Long
Public varFileName As Variant

lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
End If
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
End If

' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
strFilter = ahtAddFilterItem(strFilter, _
"Access (*.mdb)", "*.MDB;*.MDA")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
InitialDir:=varDirectory, _
Filter:=strFilter, _
Flags:=lngFlags, _
DialogTitle:=varTitleForDialog)
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
End If
GetOpenFile = varFileName
End Function
Dec 3 '09 #3
jeffstl
432 Expert 256MB
Hmm. Could be. I am not that familiar with Access but I know it should work similar to VB6.0.

So it sounds like the variable is needed not only across functions but across modules?

Regardless it looks like in your post you still have the variable declared inside of a function. It has to be declared outside the function either at the top of the module or possibly in a different module all together (which at that point it might need to be declared as Global).

Either way your solution is tied to making sure you declared it outside of any function or sub and its declared in a place that all functions and subs can access it. You might want to search like Global variable in access or sharing variable with functions in access or something like that ......there are subtle differences I know in Access syntax and VB6
Dec 3 '09 #4
OK I'm getting there! I've now got the function in the same place as the sub, so it can see the variable.

The code

MsgBox "You selected: " & ahtCommonFileOpenSave(InitialDir:="K:Finance\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Please locate the Connect file you previously saved")

This produces a message string "You selected K:Finance\Haynes etc etc etc .csv"

How do I then pass just the K:Finance etc etc etc to the TransferText command. Thanks again for your help.
Dec 3 '09 #5

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

Similar topics

18
by: Shannon Jacobs | last post by:
Trying to solve this with a regex approach rather than the programmatic approach of counting up and down the levels. I have a fairly complicated HTML page that I want to simplify. I've been able to...
0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
5
by: Sukh | last post by:
I have to design a "Online Test Application" and application is going to display question and answers.All the questons are objective type so there is four answer for each question. My Question is...
16
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument...
9
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
0
by: debjanib | last post by:
I am trying to show a FAQ questionnaire set , with set of questions and then with the answers. At First there will be a list of questions and then then will be a list of questions and answers. ...
1
by: javedna | last post by:
Can PHP help with the following as I have tried in the MYSQL Forums and cant get any help Thanks Nabz ---------------------------------------- Hi I am developing a PHP MYSQL questionnaire...
2
by: Gani | last post by:
HR Questions: Questions to ask during HR interview http://freehrquestions.blogspot.com/2008/08/questions-to-ask-during-hr-interview.html HR Interview Questions and Tips...
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.