Results 1 to 4 of 4
  1. #1
    mike02 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    245

    Import Macro Help

    Hey, I have a form with 9 text boxs:
    Address Information
    Portfolio
    Strategy
    Risk Management
    Service Providers
    Supporting Documents
    Desicion


    Monthly Data
    Daily Data
    Now I have a piece of code to display a promt box which lets me grab files from my computer. This is it:
    Code:
    Private Sub Command0_Click()
    On Error GoTo Import_Err
      
    Dim strFilter As String
    Dim strInputFileName As String
      
      strFilter = ahtAddFilterItem(strFilter, "CSV Files (*.CSV)", "*.CSV")
      strInputFileName = ahtCommonFileOpenSave( _
                    Filter:=strFilter, OpenFile:=True, _
                    DialogTitle:="Please select an input file...", _
                    Flags:=ahtOFN_HIDEREADONLY)
     'Actual import
      If Len(strInputFileName) > 0 Then
        Me.AddInfo.value = strInputFileName
        
      End If
      
    Import_Exit:
      Exit Sub
      
    Import_Err:
      MsgBox Error$
      Resume Import_Exit
    End Sub
    How could I write this to run a string through the 9 text box names, and fill the correct text box based on the Button i click to prompt this. I want to make it a function so i can just call it from each command click rather then have the same code written over and over again.
    Heres my form template:
    Address Info ................txtBox........... CmdButton(0)
    Portfolio Sec ...............txtBox........... CmdButton(1)
    Strategy Sec ................txtBox........... CmdButton(2)
    Risk Management.........txtBox........... CmdButton(3)
    Service Providers .........txtBox........... CmdButton(4)
    Supporting Documents.txtBox........... CmdButton(10)
    Desicion ......................txtBox........... CmdButton(11)
    Monthly Data................txtBox........... CmdButton(5)
    Daily Data.....................txtBox........... CmdButton(12)
    Each text box fills in the file path at which i select from the promt menu.
    At the bottom of my form I have a button called import. when i click that button i want it to import all the files selected in the txt boxes.
    Code:
    If Len(strInputFileName) > 0 Then
        DoCmd.TransferText acImportDelim, "Import Specification", "tbl_Import", strInputFileName, False, ""
        
      End If
    this doesnt work for me, I thought it could. How would I write this code to import everything (append) into the corresponding table. Reminder the txt boxes titles correspond with the tables. The CSV are in exact same format as tables IE Field names.
    Thanks In advance, really appreciate the help here.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    If you solved this thread do you care to share with others that read this Forum?

  3. #3
    mike02 is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    245
    tbh i completely scratched the idea, and am currently working on a new way. It was to busy. to much work etc. Is there away to delet threads?

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is my attempt. Only two buttons. The code is:
    Code:
    Private Sub Command22_Click()
       On Error GoTo Import_Err
    
       ' No previous control error.
       Const conNoPreviousControl = 2483
    
       Dim ctlPrevious As Control
    
       Dim strFilter As String
       Dim strInputFileName As String
       Dim TableName As String
       Dim ImpSpecName As String
    
       Set ctlPrevious = Screen.PreviousControl
    
       If ctlPrevious.ControlType = acTextBox Then
          '   MsgBox ctlPrevious.Name
    
          'get variables for import table name and import specificatiion
          '    import specificatiions must be set up manually before executing this code
          'these could be in a table for easier modifications
          Select Case ctlPrevious.Name
             Case "AddInfo"
                TableName = "Table1"
                ImpSpecName = "ImpSpec1"
             Case "Portfolio"
                TableName = "Table2"
                ImpSpecName = "ImpSpec2"
             Case "Strategy"
                TableName = "Table3"
                ImpSpecName = "ImpSpec3"
             Case "Risk"
                TableName = "Table4"
                ImpSpecName = "ImpSpec4"
             Case "Service"
                TableName = "Table5"
                ImpSpecName = "ImpSpec5"
             Case "SupportDoc"
                TableName = "Table6"
                ImpSpecName = "ImpSpec6"
             Case "Desicion"
                TableName = "Table7"
                ImpSpecName = "ImpSpec7"
             Case "MthData"
                TableName = "Table8"
                ImpSpecName = "ImpSpec8"
             Case "DayData"
                TableName = "Table9"
                ImpSpecName = "ImpSpec9"
          End Select
    
    
          strFilter = ahtAddFilterItem(strFilter, "CSV Files (*.CSV)", "*.CSV")
          strInputFileName = ahtCommonFileOpenSave( _
                             Filter:=strFilter, OpenFile:=True, _
                             DialogTitle:="Please select an input file for " & ctlPrevious.Name & "...", _
                             Flags:=ahtOFN_HIDEREADONLY)
          'Actual import
          If Len(strInputFileName) > 0 Then
             'display the file path and name on screen
             ctlPrevious = strInputFileName
             MsgBox "The table name: " & TableName & vbNewLine & "The Import spec name: " & ImpSpecName & vbNewLine & " The CSV Path & file name: " & strInputFileName
    
             'get it
             '         DoCmd.TransferText acImportDelim, ImpSpecName, TableName, strInputFileName, False
    
          End If
       Else
          MsgBox "Not a valid control"
       End If
    Import_Exit:
       Exit Sub
       '
    Import_Err:
       If Err = conNoPreviousControl Then
          MsgBox "Please select a box.", vbInformation
          Set ctlPrevious = Nothing
       Else
          MsgBox Error$
       End If
       Resume Import_Exit
    
    End Sub
    See attached A2K3 example (zipped)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Excel Import Macro Help in Access
    By vennies83 in forum Import/Export Data
    Replies: 5
    Last Post: 11-02-2011, 09:24 AM
  2. Import Excel File Using Macro?
    By oregoncrete in forum Import/Export Data
    Replies: 0
    Last Post: 04-05-2011, 12:26 PM
  3. Custom import macro
    By sliminconcoova in forum Import/Export Data
    Replies: 26
    Last Post: 09-15-2010, 10:44 AM
  4. Macro To Import CSV to ACCESS
    By csvivek in forum Import/Export Data
    Replies: 1
    Last Post: 12-07-2009, 01:49 PM
  5. import using Macro
    By johnny1g in forum Import/Export Data
    Replies: 1
    Last Post: 09-15-2009, 04:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums