+ Reply to Thread
Results 1 to 30 of 30

Textbox contents as folder/filepath

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Exclamation Textbox contents as folder/filepath

    Hi Guys,

    I need a little help with the below code
    The current code adds the contents of a textbox to the folderpath and pulls the file, But now i need the query to be in three parts:

    1). A two part folder path consisting of:

    a. (1st half ) "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    b. (2nd half) "Contents from textbox1"

    2). The file name to pull from "textbox2"



    Any help with this would be great.
    Thanks in advance

    Carl

    If FileName = vbNullString Then
    
    
      
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String: CurrentFile = Dir(strFldrPath)
    Dim FileFound As Boolean: FileFound = False
    While CurrentFile <> vbNullString And FileFound = False
    
    If InStr(1, CurrentFile, FileName, vbTextCompare) > 0 Then
    ActiveWorkbook.FollowHyperlink (strFldrPath & CurrentFile)
    FileFound = True
    
    End If
    
    CurrentFile = Dir()
    
    Wend
    
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & strFldrPath
    
    End If
    
    
    'Unload Me
    
    End Sub]

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,918

    Re: Textbox contents as folder/filepath

    Difficult to test with no sample workbook, but perhaps the below (or something like it) would do the trick:

    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\" & TextBox1 & "\" & TextBox2 'Depending on the content of TextBox2 you may need to account for the file extension here.
    BSB

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    Do you need to loop ?
    If FileName = vbNullString Then
    
    
      
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String
    Dim FileFound As Boolean: FileFound = False
    
    CurrentFile = Dir(strFldrPath & "*" & FileName & "*")
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (strFldrPath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & strFldrPath
    
    End If
    BTW, variable declared as Const MUST be constant.

  4. #4
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Guys,

    Thanks for your replies,

    Hi Jindon,

    Your code looks as if it should work but when i run it, it displays the MsgBox "No Serial number available", as if its not seeing textbox " TBM1Serial"?
    Am I doing something wrong


    Thanks
    Carl


    Private Sub CommandButton28_Click()
    
    If FileName = vbNullString Then
    
    
      
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String
    Dim FileFound As Boolean: FileFound = False
    
    CurrentFile = Dir(strFldrPath & "cmbDERS" & FileName & "TBM1Serial")
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (strFldrPath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & strFldrPath
    
    End If
    
    End Sub

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    I though "FileName" would be a global variable, since you haven't assign within procedure.

    If not, you need to add one line
    FileName = Me.TBM1Serial  '<--- this line
    If FileName = vbNullString Then
    But, yes, you still need to loop, because the user would enter just "g" or something...

  6. #6
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Jindon,

    I done as you suggested but the msg box that appears, looks like its not adding the folder path from value "cmbDERS" to the string in order to find the file in that folder? also did i mention that the file is pdf?

    Private Sub CommandButton28_Click()
    
    FileName = Me.TBM1Serial
    If FileName = vbNullString Then
    
    
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String
    Dim FileFound As Boolean: FileFound = False
    
    CurrentFile = Dir(strFldrPath & "cmbDERS" & FileName & "TBM1Serial")
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (strFldrPath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & strFldrPath
    
    End If
    
    End Sub
    regards
    carl

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    Try change
    CurrentFile = Dir(strFldrPath & "cmbDERS" & FileName & "TBM1Serial")
    to
    CurrentFile = Dir(strFldrPath & cmbDERS & "*" & Filename & "*" , 0)
    And see how it goes...

  8. #8
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Jindon,

    I'm away from home so will test it out on Friday when I get back.

    Thanks again
    Carl

  9. #9
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Jindon,

    I tried with the altered line but still no joy unfortunately.....any ideas?

    Private Sub CommandButton28_Click()
    
    FileName = Me.TBM1Serial
    If FileName = vbNullString Then
    
    
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String
    Dim FileFound As Boolean: FileFound = False
    
    CurrentFile = Dir(strFldrPath & cmbDERS & "*" & FileName & "*", 0)
    
    'CurrentFile = Dir(strFldrPath & "cmbDERS" & FileName & "TBM1Serial")
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (strFldrPath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & strFldrPath
    
    End If
    
    End Sub

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    I need to see the real value of
    MsgBox strFldrPath & cmbDERS & "*" & FileName & "*"

  11. #11
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Jindon,

    Value of cmbDERS is "a" which is the folder name

    Value of FileName is "a123" which is the file name

    I have attached a screen shot of the MsgBox that runs.

    Thanks
    Carl

    Screen grab.jpg

  12. #12
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    And with file
    Attached Images Attached Images

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    I can not see your picture... something is wrong I guess.

  14. #14
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hope this works!
    Attached Images Attached Images

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    It is OK this time..

    Can you also show me the mgsbox?
    msgbox Dir(strFldrPath & cmbDERS, 16)

  16. #16
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi,

    I only get that one msgbox shown above, even if i remove folder "a" from the "reports" folder?

    Carl

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    I mean
    Private Sub CommandButton28_Click()
    
    FileName = Me.TBM1Serial
    If FileName = vbNullString Then
    
    
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String
    Dim FileFound As Boolean: FileFound = False
    msgbox Dir(strFldrPath & cmbDERS, 16)  ' this one 
    CurrentFile = Dir(strFldrPath & cmbDERS & "*" & FileName & "*", 0)
    
    'CurrentFile = Dir(strFldrPath & "cmbDERS" & FileName & "TBM1Serial")
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (strFldrPath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & strFldrPath
    
    End If
    
    End Sub

  18. #18
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi,

    I only get that one msgbox shown, even if i remove folder "a" from the "reports" folder?

    Carl

  19. #19
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    ignore last message

  20. #20
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Second screen grab
    Attached Images Attached Images

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    Can you just try this?
    Private Sub CommandButton28_Click()
    
    FileName = Me.TBM1Serial
    If FileName = vbNullString Then
    
    
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String, myPath as string
    Dim FileFound As Boolean: FileFound = False
    mypath = strFldrPath & cmbDERS & iif(right$(cmbDERS,1)<>"\","\","")
    msgbox Dir(mypath, 16)  ' this one 
    if mypath = "" then
        msgbox "No such folder":exit sub
    endif
    CurrentFile = Dir(mypath & "*" & FileName & "*", 0)
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (mypath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & mypath
    
    End If
    
    End Sub

  22. #22
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Nearly there now

    I get the msgbox shown and if i click ok if retrieves the correct file, but if i delete folder "a" i just get a blank msgbox?
    Attached Images Attached Images

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    What do you mean by "delete folder "a"" ?

  24. #24
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    I was doing some error checking to see what would happen if folder "a" didn't exist and to see if msg "no such file" ran.

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    Try change
    mypath = strFldrPath & cmbDERS & iif(right$(cmbDERS,1)<>"\","\","")
    to
    mypath = strFldrPath & cmbDERS
    If right$(mypath,1) <> "\" then mypath = mypath & "\"

  26. #26
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Jindon,

    I tried the following but it does the same thing?

    Private Sub CommandButton142_Click()
    
    
    FileName = Me.TBM1Serial
    If FileName = vbNullString Then
    
    
    MsgBox "No Serial number available."
    Me.TBM1Serial.SetFocus
    
    Exit Sub
    End If
    
    Const strFldrPath As String = "C:\Documents and Settings\carl.walker\My Documents\Reports\"
    
    Dim CurrentFile As String, myPath As String
    Dim FileFound As Boolean: FileFound = False
    myPath = strFldrPath & cmbDERS
    If Right$(myPath, 1) <> "\" Then myPath = myPath & "\"
    MsgBox Dir(myPath, 16)  ' this one
    If myPath = "" Then
        MsgBox "No such folder":
        
        Exit Sub
        
    End If
    CurrentFile = Dir(myPath & "*" & FileName & "*", 0)
    
    If Len(CurrentFile) Then
    ActiveWorkbook.FollowHyperlink (myPath & CurrentFile)
    FileFound = True
    
    End If
    
    If FileFound = False Then
    MsgBox Title:="File Not Found", _
    Prompt:="File """ & FileName & """ not found in " & myPath
    
    End If
    
    End Sub

  27. #27
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    What is "same thing"?

  28. #28
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Sorry Jindon, I'm not explaining myself properly as usual

    When the code runs, msgbox "." appears, and only when i click "ok" it retrieves the file.
    What i need, is for it to retrieve the file without the msgbox appearing and need the msgbox to appear only if the folderpath does not exist.

    We're pretty much there now, thanks for your help so far

    Regards
    Carl

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Textbox contents as folder/filepath

    You can delete unwanted msgbox, that was a msg for me to check....

  30. #30
    Registered User
    Join Date
    02-14-2012
    Location
    Newcastle, England
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Textbox contents as folder/filepath

    Hi Jindon,

    Yes of course, what am I thinking

    Thanks for all your efforts with this, you've really helped me out

    Regards
    Carl

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change filepath in macro based on textbox in form input
    By Jenkins27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-27-2015, 09:21 PM
  2. Change filepath in macro based on textbox in form input
    By Jenkins27 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2015, 09:12 PM
  3. [SOLVED] Open folder from a filepath in cell A1
    By singerbatfink in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2015, 03:08 PM
  4. [SOLVED] Copy files from one folder to another folder (path in textbox)
    By yoko21 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2014, 02:45 AM
  5. [SOLVED] Copying files from a selected filepath to a filepath mentioned in a worksheet's cell
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-10-2012, 03:59 PM
  6. Transfering Userform textbox filepath to worksheet as a hyperlink.
    By SJJ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-29-2012, 05:17 PM
  7. Selecting a filepath based on cell contents
    By ceemo in forum Excel General
    Replies: 1
    Last Post: 06-25-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1