473,756 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Importing Spreadsheets

I presently use the following code to import an Excel Spreadsheet:

DoCmd.TransferS preadsheet acImport, SpreadsheetType :=8, _
TableName:="tbl ImportPlayerInf ormation", _
FileName:="C:\P layerInformatio n\ExportPlayerI nformation.xls" , _
Hasfieldnames:= True

Is there a way to not have the speadsheet name defined in code but to
go a specific directory and select a speadsheet to import. I import
the full contents of the spreadsheet and no matter what the
spreadsheet is called they all have the same structure and field
names?
Nov 12 '05 #1
4 4063
Paste the stuff below as a new module. Haven't the foggiest how it works,
but if you key in
FindFile("XXX", "YYY") it opens the folder dialogue box.
You can then hold the picked file as a global variable say "FileFound$ "
Then your transferdatabas e becomes
DoCmd.TransferS preadsheet acImport, SpreadsheetType :=8, _
TableName:="tbl ImportPlayerInf ormation", _
FileName:="File Found,_
Hasfieldnames:= True

Phil
Option Compare Database 'Use database order for string comparisons
Option Explicit ' Require variables to be declared before being
used.

Declare Function GetOpenFileName Lib "comdlg32.d ll" Alias _
"GetOpenFileNam eA" (pOpenfilename As OpenFilename) As Boolean
Declare Function GetSaveFileName Lib "comdlg32.d ll" Alias _
"GetSaveFileNam eA" (pOpenfilename As OpenFilename) As Boolean

Type MSA_OPENFILENAM E
' Filter string used for the Open dialog filters.
' Use MSA_CreateFilte rString() to create this.
' Default = All Files, *.*
strFilter As String
' Initial Filter to display.
' Default = 1.
lngFilterIndex As Long
' Initial directory for the dialog to open in.
' Default = Current working directory.
strInitialDir As String
' Initial file name to populate the dialog with.
' Default = "".
strInitialFile As String
strDialogTitle As String
' Default extension to append to file if user didn't specify one.
' Default = System Values (Open File, Save File).
strDefaultExten sion As String
' Flags (see constant list) to be used.
' Default = no flags.
lngFlags As Long
' Full path of file picked. When the File Open dialog box is
' presented, if the user picks a nonexistent file,
' only the text in the "File Name" box is returned.
strFullPathRetu rned As String
' File name of file picked.
strFileNameRetu rned As String
' Offset in full path (strFullPathRet urned) where the file name
' (strFileNameRet urned) begins.
intFileOffset As Integer
' Offset in full path (strFullPathRet urned) where the file extension
begins.
intFileExtensio n As Integer
End Type

Const ALLFILES = "All Files"

Type OpenFilename
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilt er As Long
nMaxCustrFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustrData As Long
lpfnHook As Long
lpTemplateName As Long
End Type

Const OFN_ALLOWMULTIS ELECT = &H200
Const OFN_CREATEPROMP T = &H2000
Const OFN_EXPLORER = &H80000
Const OFN_FILEMUSTEXI ST = &H1000
Const OFN_HIDEREADONL Y = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_NODEREFEREN CELINKS = &H100000
Const OFN_NONETWORKBU TTON = &H20000
Const OFN_NOREADONLYR ETURN = &H8000
Const OFN_NOVALIDATE = &H100
Const OFN_OVERWRITEPR OMPT = &H2
Const OFN_PATHMUSTEXI ST = &H800
Const OFN_READONLY = &H1
Const OFN_SHOWHELP = &H10

Function FindFile(strSea rchPath$, fname$) As String
' Displays the Open dialog box for the user to locate
' the Northwind database. Returns the full path to Northwind.

Dim msaof As MSA_OPENFILENAM E
Dim Extension$

' Set options for the dialog box.
msaof.strDialog Title = "Where Is " & fname & " ?"
msaof.strInitia lDir = strSearchPath
msaof.strFilter = MSA_CreateFilte rString("." & Right$(fname, 3), "",
"*.*")
msaof.strDefaul tExtension = "." & Right$(fname, 3)
msaof.strInitia lFile = fname

' Call the Open dialog routine.
MSA_GetOpenFile Name msaof

' Return the path and file name.
FindFile = Trim(msaof.strF ullPathReturned )

End Function
Function MSA_CreateFilte rString(ParamAr ray varFilt() As Variant) As String
' Creates a filter string from the passed in arguments.
' Returns "" if no argumentss are passed in.
' Expects an even number of argumentss (filter name, extension), but
' if an odd number is passed in, it appends "*.*".

Dim strFilter As String
Dim intRet As Integer
Dim intNum As Integer

intNum = UBound(varFilt)
If (intNum <> -1) Then
For intRet = 0 To intNum
strFilter = strFilter & varFilt(intRet) & vbNullChar
Next
If intNum Mod 2 = 0 Then
strFilter = strFilter & "*.*" & vbNullChar
End If

strFilter = strFilter & vbNullChar
Else
strFilter = ""
End If
MSA_CreateFilte rString = strFilter
End Function

Private Function MSA_GetOpenFile Name(msaof As MSA_OPENFILENAM E) As Integer
' ******
' Opens the Open dialog.

Dim of As OpenFilename
Dim intRet As Integer

MSAOF_to_OF msaof, of
intRet = GetOpenFileName (of)
If intRet Then
OF_to_MSAOF of, msaof
End If
MSA_GetOpenFile Name = intRet
End Function

Private Sub OF_to_MSAOF(of As OpenFilename, msaof As MSA_OPENFILENAM E)
' ******
' This sub converts from the Win32 structure to the Microsoft Access
structure.

msaof.strFullPa thReturned = Left(of.lpstrFi le, InStr(of.lpstrF ile,
vbNullChar) - 1)
msaof.strFileNa meReturned = of.lpstrFileTit le
msaof.intFileOf fset = of.nFileOffset
msaof.intFileEx tension = of.nFileExtensi on
End Sub

Private Sub MSAOF_to_OF(msa of As MSA_OPENFILENAM E, of As OpenFilename)
' ******
' This sub converts from the Microsoft Access structure to the Win32
structure.

Dim strFile As String * 512

' Initialize some parts of the structure.
of.hwndOwner = Application.hWn dAccessApp
of.hInstance = 0
of.lpstrCustomF ilter = 0
of.nMaxCustrFil ter = 0
of.lpfnHook = 0
of.lpTemplateNa me = 0
of.lCustrData = 0

If msaof.strFilter = "" Then
of.lpstrFilter = MSA_CreateFilte rString(ALLFILE S)
Else
of.lpstrFilter = msaof.strFilter
End If
of.nFilterIndex = msaof.lngFilter Index

of.lpstrFile = msaof.strInitia lFile _
& String(512 - Len(msaof.strIn itialFile), 0)
of.nMaxFile = 511

of.lpstrFileTit le = String(512, 0)
of.nMaxFileTitl e = 511

of.lpstrTitle = msaof.strDialog Title

of.lpstrInitial Dir = msaof.strInitia lDir

of.lpstrDefExt = msaof.strDefaul tExtension

of.flags = msaof.lngFlags

of.lStructSize = Len(of)
End Sub

"Glen" <gl**********@s haw.ca> wrote in message
news:30******** *************** **@posting.goog le.com... I presently use the following code to import an Excel Spreadsheet:

DoCmd.TransferS preadsheet acImport, SpreadsheetType :=8, _
TableName:="tbl ImportPlayerInf ormation", _
FileName:="C:\P layerInformatio n\ExportPlayerI nformation.xls" , _
Hasfieldnames:= True

Is there a way to not have the speadsheet name defined in code but to
go a specific directory and select a speadsheet to import. I import
the full contents of the spreadsheet and no matter what the
spreadsheet is called they all have the same structure and field
names?

Nov 12 '05 #2
Glen wrote:
I presently use the following code to import an Excel Spreadsheet:

DoCmd.TransferS preadsheet acImport, SpreadsheetType :=8, _
TableName:="tbl ImportPlayerInf ormation", _
FileName:="C:\P layerInformatio n\ExportPlayerI nformation.xls" , _
Hasfieldnames:= True

Is there a way to not have the speadsheet name defined in code but to
go a specific directory and select a speadsheet to import. I import
the full contents of the spreadsheet and no matter what the
spreadsheet is called they all have the same structure and field
names?


Leave the File Name parameter blank, you will be prompted for the file
name (a File Open dialog box).

--
MGFoster:::mgf
Oakland, CA (USA)

Nov 12 '05 #3
Thanks for the reply, however

I first tried to leave the FileNmae:= as this which caused a compile
error.

I have removed the file name and installed "".

FileName:=""

Nothing happened.

I removed the FileName:= completely, nothing happened.

What am I missing

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You're not doing anything wrong. I was wrong! Apparently I was
thinking of another method. Sorry.

- --
MGFoster:::mgf
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP77/FIechKqOuFEgEQI HTQCguAh5g71ABH akJAE13mq/Qe2N4qMAn2eA
Gepb+kgZ4wnZeLu LiI6w2da3
=XhYe
-----END PGP SIGNATURE-----
Glen Johnson wrote:
Thanks for the reply, however

I first tried to leave the FileNmae:= as this which caused a compile
error.

I have removed the file name and installed "".

FileName:=""

Nothing happened.

I removed the FileName:= completely, nothing happened.

What am I missing

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3418
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for the last field, it only checks the top few 'cells' to see if there is any data, if not, the field is not imported). How do I 'force' Access to import the field, regardless if there is data in the top of the field or not? For instance, I might...
0
1208
by: OM | last post by:
I have 3 tables tblFI, tblFIDet, tblMac, in Access 2000 tblFI fldFIAuto, autonumber, fldDate, date
2
1383
by: OM | last post by:
I have 3 tables tblFI, tblFIDet, tblMac, in Access 2000 tblFI fldFIAuto, autonumber, fldDate, date …and a few more fields that don’t matter. tblFIDet fldFIDetAutoID, autonumber fldFIAutoID, linked to tblFI.fldFIAutoID
2
1643
by: RustyR | last post by:
Hi, I have 5 excel spreadsheets that are exported to me weekly. At first, I could not get them to import. Then I did a File -> Save as and noticed that the Filenames of the spreadsheets contained Quotes around them - hence the problem. Is there any way to remove those quotes prior to import (other than "Save As")???
1
1610
by: jfallara | last post by:
Hi all, kind of new to Access, but I'm learning as I go. What I'm currently trying to do is take data from an Excel spreadsheet and transfer it into an Access database with mutiple linked tables. The catch is, columns from the one spreadsheet are spread out over all 6 of my linked tables. e.g. 2 columns go to 2 fields in table A, another column goes to a field in table B, 2 more go to 2 fields in table C...etc. I can import the whole...
2
3610
by: nutthatch | last post by:
I want to be able to import an Excel spreadsheet into Access 2K using the macro command Transferspreadsheet. However, the file I am importing (over which I have no control) contains some records that are "dirty" i.e. the field contents do not comply with the expected format (date/time) and they end up in a seperate table of import errors. (The records in "error" are actually empty fields.) This is a regular event and I do not want to...
0
767
by: DrewYK | last post by:
I have the project that may never end in front of me. I am creating a routine that will take SpreadSheets from Excel and bring them into Access. I am not using any "DoCmd"s because the goal is for the import code to be moved to a stand alone VB app which will use the Access DB as a workspace to process the data from the spreadsheets. Quite honestly, done right this may not even require Access or Excel to be on the users machine. ...
1
5163
by: thadson | last post by:
Hi, I'm trying to import specific cells from MS Excel 2000 spreadsheets to MS Access 2000 tables then move the spreadsheets to a different directory. I'm very new to this and I'm having trouble to implement this. I have worked out so far the code to import certain cells into 1 table, but I do not know how to import some other cells into another tables so the data would be connected and remain together. So lets say that I have 2 tables...
3
1549
by: Umoja | last post by:
Hi All, This seems like a simple thing, but I can’t seem to figure it out. I am familiar with transferring excel spreadsheets into access using the TransferSpreasheet function Ex:(DoCmd.TransferSpreadsheet acImport, 8, "LogTemplate", "C:\Documents and Settings\My Documents\Log template.xls", True, ""). I would like to import several spreadsheets into access without creating the table. I know how to manually import the spreadsheet but how...
0
9287
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.