Results 1 to 7 of 7
  1. #1
    IncidentalProgrammer is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2014
    Location
    Texas
    Posts
    156

    Using user-entered data in unbound fields as Variables to build a phrase.

    So the books are great at telling me how to use input boxes to collect data to use as variables to build a sentence, but not so much on using actual form fields. I am beating my head against the wall!



    I have a very small form accessed by a button on one of my main forms, who's sole purpose is to generate file names to be copied and pasted onto files in the shared drive. My boss has a very strict naming convention for these files, and this is just one of those nifty little features that I wanted to add to our database to help with uniformity. The form takes the policy number and date from the main Policy form it was entered through, and then there are unbound fields for the user to enter an endorsement number, and up to 5 short descriptive items (in combo boxes).

    Upon clicking a button, the procedure needs to take that data, and format it into a file name that the user can copy. I'm having some trouble pulling the data into variables, though. I've tried a couple of different formats, and here's where I'm at now:
    Code:
    Private Sub btnFileName_Click()
         'Generates file name based on data in the generator.
         'Declare variables.
        Dim PolNum As String
        Dim EndNum As Byte
        Dim EffDate As Date
        Dim desc1 As String
        Dim desc2 As String
        Dim desc3 As String
        Dim desc4 As String
        Dim desc5 As String
        
        PolNum = Forms!frmFileName!ctlPolNum
        EndNum = Forms!frmFileName!ctlEndNum
        EffDate = Forms!frmFileName!ctlPolEffDt
        desc1 = Forms!frmFileName!ctlDesc1
        desc2 = Forms!frmFileName!ctlDesc2
        desc3 = Forms!frmFileName!ctlDesc3
        desc4 = Forms!frmFileName!ctlDesc4
        desc5 = Forms!frmFileName!ctlDesc5
    I'm also having some difficulties formatting the naming structure into code. It goes like this: policynumber(space)(space)endorsementnumber(space) (space)effectivedate(space)(space)descriptions. Again, the books are great about telling me how to do this in a message box...but I need this to populate another unbound text field on that form. Is this stuff doable?

    Also, she wants the dates formatted as YYYY.MM.DD. Can I do that with code?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It would help to see an example of a file name. But try this

    ****Untested****
    Code:
    Private Sub btnFileName_Click()
         'Generates file name based on data in the generator.
         'Declare variables.
       
        dim sFileName as string
    
        Dim PolNum As String
        Dim EndNum As Byte
        Dim EffDate As Date
        Dim desc1 As String
        Dim desc2 As String
        Dim desc3 As String
        Dim desc4 As String
        Dim desc5 As String
        
        PolNum = Forms!frmFileName!ctlPolNum
        EndNum = Forms!frmFileName!ctlEndNum
        EffDate = Forms!frmFileName!ctlPolEffDt
        desc1 = Forms!frmFileName!ctlDesc1
        desc2 = Forms!frmFileName!ctlDesc2
        desc3 = Forms!frmFileName!ctlDesc3
        desc4 = Forms!frmFileName!ctlDesc4
        desc5 = Forms!frmFileName!ctlDesc5
    
       ' policynumber(space)(space)endorsementnumber(space) (space)effectivedate(space)(space)descriptions
        sFileName = PolNum & "  " & EndNum & "  " & Format(EffDate, "YYYY.MM.DD") & "  " & desc1 & desc2 & desc3 & desc4 & desc5 
    
        Me.TextBoxName = sFileName
    
    End Sub

    Tell your boss (or show her this) that having spaces in names is not good practice. If you MUST have separation between parts of the name, use the underscore:

    sFileName = PolNum & "__" & EndNum & "__" & Format(EffDate, "YYYY, MM, DD") & "__" & desc1 & desc2 & desc3 & desc4 & desc5

    Much easier name to work with!


    If this is going to be a computer file name, the dots in the date are going to also be a problem. I'm just saying...........

  3. #3
    hansendl is offline Advanced Hobbyist
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    38
    Are you having trouble with pulling the data from all of the controls into variables, or only certain types of controls (e.g., just the combo boxes)?

    Also, as an aside, you may want to consider using the Me reference in your code instead of the full form reference to point to your controls--e.g., use "Me.ctlDesc1" instead of "Forms!frmFileName!ctlDesc1". This way, you won't have to rewrite your code if you ever change the form name--and it's just easier to type!

  4. #4
    IncidentalProgrammer is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2014
    Location
    Texas
    Posts
    156
    It works! Thanks so much, Ssanfu!

    An example would be something like: "123456789 End#01 2014.10.21 Renewal, Add ETS, Add Privacy Notice"

    The only problems I'm having with it now are when I put the endorsement number in, it won't take the 0 if 0 is the first digit (like 03). I tried using Format like you did for the date, but would it be ## to tell it to use a 2 digit number? Or am I off on that? And is there a way to make the descriptions optional? It gives me an error if I don't use all 5, but they won't always all be needed. (Sorry, I am SO new to all of this.)

    She's very particular about the names, and when I started saving these documents to the file, she told me she didn't want underscores. She has something against underscores. I am not in IT, so I didn't know that could cause problems. The names I'm getting the database to generate are not going to be stored or used in the database itself, they're made for the sole purpose of copying and pasting onto PDFs in client folders on the server. I know about spaces in names in Access and code being problems, but would they cause problems on PDFs? And the dots too? I know we use those because we can't put backslashes in the names on our PDFs.


    Thanks, Hansendl! I changed them to Me.'s, and yeah, that's so much easier to type. I was kind of basing this off some code I found in a tutorial online, and they were using the Forms! setup.

  5. #5
    IncidentalProgrammer is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2014
    Location
    Texas
    Posts
    156
    Actually, I just figured out how to make it handle there being no data in some of the descritptive fields. So the only thing I'm still struggling with on it is getting that endorsement number to show two digits when the first is a 0.

  6. #6
    hansendl is offline Advanced Hobbyist
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    38
    Check out the Format function. Using that, I believe you can include the leading zero.

  7. #7
    IncidentalProgrammer is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2014
    Location
    Texas
    Posts
    156
    Got it! Thanks so much, y'all!

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

Similar Threads

  1. Replies: 1
    Last Post: 08-28-2014, 10:44 PM
  2. Replies: 15
    Last Post: 04-17-2014, 02:21 PM
  3. Replies: 1
    Last Post: 06-20-2013, 05:06 PM
  4. Replies: 2
    Last Post: 07-30-2012, 03:26 PM
  5. User entered data on report
    By funkygoorilla in forum Reports
    Replies: 1
    Last Post: 02-18-2012, 01:59 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