Results 1 to 7 of 7
  1. #1
    nycman is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Oct 2012
    Posts
    31

    Adding specific text in table field record

    Hello,



    I use the following function to add a next class name into the tblClass, ClassName field.

    Code:
     'function returns the next Class Name for frmAddNewClass form
    
    Public Function GetNum() As String
     Dim STR As String
      'no records from current year
       If IsNull(DLookup("ClassName", "tblClass", _
          "left(ClassName,2)='" & Right(CStr(Year(Date)), 2) & "' ")) Then
            GetNum = Right(CStr(Year(Date)), 2) & "-" & "001"
       Else
          STR = CStr(CInt(Right(DMax("ClassName", "tblClass", _
              "left(ClassName,2)='" & Right(CStr(Year(Date)), 2) & "' "), 3)) + 1)
          GetNum = Right(CStr(Year(Date)), 2) & "-" & ZeroLeft(STR, 3)
       End If
    End Function
    This will result in ClassName being 15-001, 15-002....

    How do I add a specific text (CLASS) to appear before the 15-001, like CLASS 15-001, CLASS 15-002 etc.

    Thank you

  2. #2
    nycman is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Oct 2012
    Posts
    31
    [QUOTE=nycman;291798
    How do I add a specific text (CLASS) to appear before the 15-001, like CLASS 15-001, CLASS 15-002 etc.
    [/QUOTE]

    Ok Got it.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Because there wasn't an example, I wasn't sure if you meant the word "Class" or a class name such as "Math".

    What does your function look like now?

  4. #4
    nycman is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Oct 2012
    Posts
    31
    Quote Originally Posted by ssanfu View Post
    Because there wasn't an example, I wasn't sure if you meant the word "Class" or a class name such as "Math".

    What does your function look like now?
    Sorry I meant the word Class.

    Code:
    Public Function GetNum() As String
     Dim STR As String
      'no records from current year
       If IsNull(DLookup("ClassName", "tblClass", _
          "left(ClassName,2)='" & Right(CStr(Year(Date)), 2) & "' ")) Then
            GetNum = "Class" & " " & Right(CStr(Year(Date)), 2) & "-" & "001"
       Else
          STR = CStr(CInt(Right(DMax("ClassName", "tblClass", _
              "left(ClassName,2)='" & Right(CStr(Year(Date)), 2) & "' "), 3)) + 1)
          GetNum = "Class" & " " & Right(CStr(Year(Date)), 2) & "-" & ZeroLeft(STR, 3)
       End If
    End Function

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    One suggestion:
    Don't need the quoted space - just add it to the back of the word "Class" (doesn't hurt, but easier to add the space to the word)
    From
    Code:
    GetNum = "Class" & " " & Right(CStr(Year(Date)), 2) & "-" & ZeroLeft(STR, 3)
    to
    Code:
    GetNum = "Class " & Right(CStr(Year(Date)), 2) & "-" & ZeroLeft(STR, 3)


  6. #6
    nycman is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Oct 2012
    Posts
    31
    Quote Originally Posted by ssanfu View Post
    One suggestion:
    Don't need the quoted space - just add it to the back of the word "Class" (doesn't hurt, but easier to add the space to the word)
    From
    Code:
    GetNum = "Class" & " " & Right(CStr(Year(Date)), 2) & "-" & ZeroLeft(STR, 3)
    to
    Code:
    GetNum = "Class " & Right(CStr(Year(Date)), 2) & "-" & ZeroLeft(STR, 3)

    Thanks, small problem though. This function fires on Form OnLoad so now if I open this form and then close it the record gets saved in the tblClass immediately. I want the user to be asked if he/she want's the record to be saved Yes/No. I tried putting the code in On Dirty but that did not help. I also tried putting

    Code:
    If MsgBox("Do you want to save this record?", vbYesNo, "Record Change") = vbYes Then
          DoCmd.RunCommand acCmdSaveRecord
    Else
          Cancel = True
     End If
    in the Form On Close but still no results.

  7. #7
    nycman is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Oct 2012
    Posts
    31
    Ok Got this one too!

    On Before Update Event

    Dim strMsg As String
    Dim iResponse As Integer


    ' Specify the message to display.
    strMsg = "Do you wish to save the changes?" & Chr(10)
    strMsg = strMsg & "Click Yes to Save or No to Discard changes."


    ' Display the message box.
    iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")

    ' Check the user's response.
    If iResponse = vbNo Then

    ' Undo the change.
    DoCmd.RunCommand acCmdUndo


    ' Cancel the update.
    Cancel = True
    End If

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

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2013, 09:58 AM
  2. Replies: 6
    Last Post: 11-15-2012, 05:38 PM
  3. copying a record and adding in new text
    By will_clarke in forum Access
    Replies: 1
    Last Post: 01-12-2012, 11:08 AM
  4. Replies: 11
    Last Post: 06-02-2011, 10:21 AM
  5. Text Box to show specific record
    By chu3w in forum Forms
    Replies: 1
    Last Post: 04-01-2010, 12:23 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