Results 1 to 6 of 6
  1. #1
    govblaine is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    3

    how to convert VBA written for excel to access


    I need help, I found some VBA from an excel workbook that I want to be able to use in access. It currently works fine from the excel workbook but I need to use it in access. I have no experience with VBA in Access and I can only think the same code can be used but needs to be tweaked for access. The code looks for information inside files in a folder and renames the files based on the value found at a certain position in the file. It also adds he extension ".txt". Can anyone help make this code work in access?

    Thank you

    Code:
    Function RenameTextFiles()
    Dim fso As FileSystemObject
    Dim x As Integer
    Dim strPortCode As String
    Dim strfile As String
    Dim TextLine
    Dim strFullFileName As String
    Dim FILEPATH As String
    Dim FILEPATH2 As String
    
    FILEPATH = "C:\Data\"
    FILEPATH2 = "C:\TempFiles\"
    
    Dim pathcrnt As String, batch file As Integer
    pathcrnt = ActiveWorkbook.Path
    batch_file = FreeFile()
    Set fso = CreateObject("Scripting.FileSystemObject")
    strfile = Dir(FILEPATH)
     
    Do While strfile <> ""
      x = 1
      strFullFileName = FILEPATH & strfile
      Open strFullFileName For Input As #1 ' Open file.
      Do While Not EOF(1) ' Loop until end of file.
        Line Input #1, TextLine ' Read line into variable.
        If x = 48 Then
          strPortCode = Mid(TextLine, 18, 4)
          Exit Do
        End If
        x = x + 1
      Loop
      Close #1 ' Close file.
      If Left(fso.GetFileName(strfile), 5) = "PCOSS" Then
        If Not fso.FileExists(FILEPATH & "COSS" & strPortCode & ".txt") Then
            Name FILEPATH & strfile As FILEPATH & "COSS" & strPortCode & ".txt"
        End If
      End If
      strfile = Dir
    Loop
     
     
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    It should work the same for access, unless you have Excel objects.
    which Is the workbook.. This is all file ops.
    paste into a module.

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Yeah, I don't see anything that is Excel specific, either.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You will probably need to set a reference to "Microsoft Scripting RunTime".


    Code:
    Dim pathcrnt As String, batch file As Integer
    "batch file" has a space in the name. There is a line
    Code:
    batch_file = FreeFile()
    but it is not used, so both lines can be removed/commented out.
    Also, the line
    Code:
    pathcrnt = ActiveWorkbook.Path
    should be removed/commented out. Access does not have an "ActiveWorkbook"

    Should also probably add
    Code:
    Set fso = Nothing
    after the last "Loop"
    Code:
        Loop
    
        Set fso = Nothing
    
    End Function

  5. #5
    govblaine is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    3
    Quote Originally Posted by ssanfu View Post
    You will probably need to set a reference to "Microsoft Scripting RunTime".


    Code:
    Dim pathcrnt As String, batch file As Integer
    "batch file" has a space in the name. There is a line
    Code:
    batch_file = FreeFile()
    but it is not used, so both lines can be removed/commented out.
    Also, the line
    Code:
    pathcrnt = ActiveWorkbook.Path
    should be removed/commented out. Access does not have an "ActiveWorkbook"

    Should also probably add
    Code:
    Set fso = Nothing
    after the last "Loop"
    Code:
        Loop
    
        Set fso = Nothing
    
    End Function

    ssanfu, I made all the changes you suggested to cleaned up the code and set the reference. now the code seems to work. Thank you all very much for your time and responses.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I ran the code as a Sub instead of Function - no error.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 8
    Last Post: 11-25-2015, 11:38 AM
  2. Using VBA originally written for Excel Workbook in Access
    By AccessRookee in forum Programming
    Replies: 4
    Last Post: 12-10-2014, 12:54 PM
  3. convert functions Excel to access
    By azhar2006 in forum Queries
    Replies: 2
    Last Post: 07-25-2014, 12:33 PM
  4. Convert Excel to Access
    By wkenddad in forum Database Design
    Replies: 1
    Last Post: 04-19-2012, 01:55 AM
  5. Convert Excel Macro to Access Query
    By crownedzero in forum Queries
    Replies: 1
    Last Post: 06-22-2009, 02:13 PM

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