Results 1 to 15 of 15
  1. #1
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9

    Exclamation Access VBA issues

    I have inherited an Access DB and need to modify it a little.



    This is a database that can manage several programs, and each program can have multiple items associated with it. There is a letter that is created after you enter all the data. One part of the letter is a Transmittal letter number (XMTL Number) that is currently getting auto generated with VBA code.

    I need to stop having it auto generate the code and allow the end user to enter what the code will be. But there is a prefix that goes before the number that is already established in the Access DB.

    Click image for larger version. 

Name:	Main Menu.png 
Views:	39 
Size:	22.6 KB 
ID:	47359
    Here is the image of the main window. Once you select a program name then all the buttons are activated.

    Click image for larger version. 

Name:	Program Info.png 
Views:	39 
Size:	48.0 KB 
ID:	47360

    From the Main Menu if you select the Program Update button you will get this window where the prefix number is filled in and where it will get the next number to auto generate when you go to create a letter.

    Click image for larger version. 

Name:	Create Letter.png 
Views:	39 
Size:	70.7 KB 
ID:	47361
    On the Create letter form, is the XMTL Number field that is auto generated from the Program Update form. This field does not allow me to update or edit it.

    Click image for larger version. 

Name:	XMTL Number Field-Got Focus Field.jpg 
Views:	40 
Size:	120.3 KB 
ID:	47362

    This is the code behind the XMTL Number Field, and I don't know what needs to be modified to keep the prefix but allow me to change the number after the prefix.

    If you need more information, please ask.

    Thanks for your guidance

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    better to post you code by copy/paste and use the code tags as previously advised.

    So I can only describe where to look.

    towards the bottom of the code you assign Xprefix & xchar. Think this only needs to be Xprefix

  3. #3
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9

    Access Database XMTL Number

    Here is the code for the XMTL Number Got Focused field. The DB is on my work computer, so I have to capture the info and send to my home computer.

    Code:
    Private Sub XMTL_Number_GotFocus()
     
     
        Dim XValue As String
        Dim Xprefix As String
        Dim XChar As String
        Dim XAdmin As String
        Dim XContact As String
        Dim XPhone As String
        Dim Criteria As String
        Dim MyDB As Database
        Dim MySet As Recordset
        Dim NewNumber As Integer
     
        If IsNull(Me![Program]) Then Me![Program] = Forms![DM Main Menu]![Program]
        If IsNull(Me![XMTL Date]) Then Me![XMTL Date] = Format(Date, "DD-MMM-YYYY")
     
        If IsNull(Me![XMTL Number]) Then
            Criteria = "Program = '" & Forms![DM Main Menu]![Program] & "'"
           
            Set MyDB = CurrentDb()
            Set MySet = MyDB.OpenRecordset("Program", DB_OPEN_DYNASET)
            MySet.FindFirst Criteria              ' Find Program in table
            XValue = MySet![XMTL Number]
            XAdmin = MySet![Administrator]
            XContact = MySet![Contact]
            If Not IsNull(MySet![Contact Phone]) Then
               XPhone = MySet![Contact Phone]
            End If
     
            If Not IsNull(MySet![XMTL Prefix]) Then
                Xprefix = MySet![XMTL Prefix]
            Else
                Xprefix = ""
            End If
     
            NewNumber = (XValue + 1)
            MySet.Edit                            ' Enable editing
            MySet![XMTL Number] = NewNumber       ' Change XMTL Number
            MySet.Update                          ' Save changes
            MySet.Close                           ' Close RecordSet
       
            XChar = "" & XValue
            If (XValue > 0 And XValue < 10) Then XChar = "000" & XValue
            If (XValue > 9 And XValue < 100) Then XChar = "00" & XValue
            If (XValue > 99 And XValue < 1000) Then XChar = "0" & XValue
            [XMTL Number] = Xprefix & XChar
            [Administrator] = XAdmin        ' Store Admin Name
            [Contact] = XContact            ' Store Contact Info
            If Not IsNull(XPhone) Then
               [Contact Phone] = XPhone
            End If
        End If
     
        Forms![DM Main Menu]![XMTL Number] = [XMTL Number]
        DoCmd.GoToControl "XMTL Date"
     
    End Sub

    Quote Originally Posted by Ajax View Post
    better to post you code by copy/paste and use the code tags as previously advised.

    So I can only describe where to look.

    towards the bottom of the code you assign Xprefix & xchar. Think this only needs to be Xprefix

  4. #4
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9
    How do i change the code so that the XMTL Number field is editable? So that I can enter the suffix?

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    suspect nothing to do with the code - is the control enabled and unlocked?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,275
    Posted in error
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9
    The XMTL Number field is not locked.

    I tried entering the number in the Next Transmittal Number field so it will use it for the next transmittal letter but I get an error.

    Click image for larger version. 

Name:	Program Info.png 
Views:	15 
Size:	48.0 KB 
ID:	47391
    Click image for larger version. 

Name:	error.png 
Views:	15 
Size:	5.7 KB 
ID:	47392
    Click image for larger version. 

Name:	XMTL Number Field-Got Focus Field.jpg 
Views:	15 
Size:	120.3 KB 
ID:	47393

    I really need help to try and figure out how to enter my own suffix to the XMTL number and store that number in DB. I have been wondering if there is a size limit on the field or table or could there be a type mismatch in the code?
    Thank you for your help.

  8. #8
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9
    Click image for larger version. 

Name:	error code.png 
Views:	15 
Size:	65.2 KB 
ID:	47394

    Here is the correct pic of when I got the error code where it stopped.

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    I'm going to repeat what Ajax said -
    better to post you code by copy/paste and use the code tags as previously advised.
    Until I get a raise in pay here, I'm not even going to look at pictures of code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9
    Code:
    Private Sub XMTL_Number_GotFocus()
     
     
        Dim XValue As String
        Dim Xprefix As String
        Dim XChar As String
        Dim XAdmin As String
        Dim XContact As String
        Dim XPhone As String
        Dim Criteria As String
        Dim MyDB As Database
        Dim MySet As Recordset
        Dim NewNumber As Integer
     
        If IsNull(Me![Program]) Then Me![Program] = Forms![DM Main Menu]![Program]
        If IsNull(Me![XMTL Date]) Then Me![XMTL Date] = Format(Date, "DD-MMM-YYYY")
     
        If IsNull(Me![XMTL Number]) Then
            Criteria = "Program = '" & Forms![DM Main Menu]![Program] & "'"
           
            Set MyDB = CurrentDb()
            Set MySet = MyDB.OpenRecordset("Program", DB_OPEN_DYNASET)
            MySet.FindFirst Criteria              ' Find Program in table
            XValue = MySet![XMTL Number]
            XAdmin = MySet![Administrator]
            XContact = MySet![Contact]
            If Not IsNull(MySet![Contact Phone]) Then
               XPhone = MySet![Contact Phone]
            End If
     
            If Not IsNull(MySet![XMTL Prefix]) Then
                Xprefix = MySet![XMTL Prefix]
            Else
                Xprefix = ""
            End If
     
            NewNumber = (XValue + 1)
            MySet.Edit                            ' Enable editing
            MySet![XMTL Number] = NewNumber       ' Change XMTL Number
            MySet.Update                          ' Save changes
            MySet.Close                           ' Close RecordSet
       
            XChar = "" & XValue
            If (XValue > 0 And XValue < 10) Then XChar = "000" & XValue
            If (XValue > 9 And XValue < 100) Then XChar = "00" & XValue
            If (XValue > 99 And XValue < 1000) Then XChar = "0" & XValue
            [XMTL Number] = Xprefix & XChar
            [Administrator] = XAdmin        ' Store Admin Name
            [Contact] = XContact            ' Store Contact Info
            If Not IsNull(XPhone) Then
               [Contact Phone] = XPhone
            End If
        End If
     
        Forms![DM Main Menu]![XMTL Number] = [XMTL Number]
        DoCmd.GoToControl "XMTL Date"
     
    End Sub

  11. #11
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9
    I pasted the code above. The DB is on my work computer so I have to email it to myself so I can paste it here. Sorry

    I appreciate you help.

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    step through the code and check values as you go.

    It may be something to do with you appear to have a table with a field with the same name (program).

    Would also help if you clarified what is what, better still use meaningful names or at least document your code to explain. is Xvalue supposed to be Next Transmittal Number?

  13. #13
    lgvalencia is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2022
    Posts
    9
    I inherited this code, so I don't know everything it is doing. that is why I am seeking help

  14. #14
    usfarang is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2021
    Posts
    42
    Simply rem out the call to the procedure if your not going to use it?
    Or if your ONLY going to use part of it, rem out the other portions!
    Lastly take a look at your table(s) and see what is there.
    hth

  15. #15
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    I don't know everything it is doing
    We can tell you what the code is doing, but without the context of the whole app, difficult to say whether any bit matters. You might be better advised to find an Access developer, provide them with the app and explain what it is supposed to do, if only from a user perspective. If you insist on doing it yourself, I've notated some of your code which may help you get started

    Code:
    Private Sub XMTL_Number_GotFocus()
     
     
        Dim XValue As String 'why string? apparently should be a number? relates to Next Transmittal number which now wants to be manually entered rather than calculated
        Dim Xprefix As String
        Dim XChar As String
        Dim XAdmin As String
        Dim XContact As String
        Dim XPhone As String
        Dim Criteria As String
        Dim MyDB As Database
        Dim MySet As Recordset
        Dim NewNumber As Integer
     
        If IsNull(Me![Program]) Then Me![Program] = Forms![DM Main Menu]![Program] 'where is this form? is the program value populated?
        If IsNull(Me![XMTL Date]) Then Me![XMTL Date] = Format(Date, "DD-MMM-YYYY") ' what datatype is XMTL Date? if date? why make it a string? can this be manually entered or is the control locked?
     
        If IsNull(Me![XMTL Number]) Then
            Criteria = "Program = '" & Forms![DM Main Menu]![Program] & "'"
           
            Set MyDB = CurrentDb()
            Set MySet = MyDB.OpenRecordset("Program", DB_OPEN_DYNASET) 'why not simply apply criteria to the program and not use findfirst? i.e. "SELECT * FROM Program WHERE " & Criteria
            MySet.FindFirst Criteria              ' Find Program in table 'what happens if the criteria is not met, so no record found?
            XValue = MySet![XMTL Number]
            XAdmin = MySet![Administrator]
            XContact = MySet![Contact]
            If Not IsNull(MySet![Contact Phone]) Then
               XPhone = MySet![Contact Phone]
            End If
     
            If Not IsNull(MySet![XMTL Prefix]) Then
                Xprefix = MySet![XMTL Prefix]
            Else
                Xprefix = ""
            End If
     
            NewNumber = (XValue + 1) 'Xvalue is a string
            MySet.Edit                            ' Enable editing
            MySet![XMTL Number] = NewNumber       ' Change XMTL Number
            MySet.Update                          ' Save changes
            MySet.Close                           ' Close RecordSet
       
            XChar = "" & XValue
            If (XValue > 0 And XValue < 10) Then XChar = "000" & XValue 'xvalue is a string, use format(XValue,"0000") then other lines not required
            If (XValue > 9 And XValue < 100) Then XChar = "00" & XValue 'xvalue is a string
            If (XValue > 99 And XValue < 1000) Then XChar = "0" & XValue 'xvalue is a string
            [XMTL Number] = Xprefix & XChar
            [Administrator] = XAdmin        ' Store Admin Name
            [Contact] = XContact            ' Store Contact Info
            If Not IsNull(XPhone) Then
               [Contact Phone] = XPhone
            End If
        End If
     
        Forms![DM Main Menu]![XMTL Number] = [XMTL Number]
        DoCmd.GoToControl "XMTL Date"
     
    End Sub

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

Similar Threads

  1. Replies: 3
    Last Post: 04-11-2013, 06:53 AM
  2. Access Database Issues
    By Frankie_B in forum Access
    Replies: 1
    Last Post: 01-31-2013, 04:41 AM
  3. Access compatibility issues?
    By YounesB3 in forum Access
    Replies: 8
    Last Post: 10-02-2012, 03:38 PM
  4. Basic access issues
    By c2bme in forum Access
    Replies: 1
    Last Post: 03-22-2010, 09:03 AM
  5. Access security issues
    By Zeallord in forum Security
    Replies: 2
    Last Post: 04-17-2009, 05:11 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