Results 1 to 3 of 3
  1. #1
    whitelockben is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    5

    On click textbox inserts value back on combobox

    Hi all,

    I have something I want to add though and this is that on click in a text box the text box will look at another value in a combobox and based on that value with insert a specific letter.



    E.g. If cboT.Value = "Electical" a P would be instered into the text box.

    This field will be my key field and everything will be unique however I also want the field to auto insert the next number.

    It will be made up of a number prefixed by either P or IC. So I want the db to search for the number from the record previous with the same prefix and use the next number.

    Does anyone know if this is possible?

  2. #2
    Toyman is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    233
    Quote Originally Posted by whitelockben View Post
    Hi all,

    I have something I want to add though and this is that on click in a text box the text box will look at another value in a combobox and based on that value with insert a specific letter.

    E.g. If cboT.Value = "Electical" a P would be instered into the text box.

    This field will be my key field and everything will be unique however I also want the field to auto insert the next number.

    It will be made up of a number prefixed by either P or IC. So I want the db to search for the number from the record previous with the same prefix and use the next number.

    Does anyone know if this is possible?
    Put the following code in the "on click" event of the text box

    Dim sPrefix as string
    Dim Mysql as string
    Dim sKey as string
    Dim sSuffix as Integer
    Dim X as Integer
    Dim conn as New Adodb.Connections
    Dim rs as New Adodb.Recordset

    'assigns the prefix for the key based on cboTSelect case Me.cboT
    Case "Electrical"
    sPrefix = "P"
    X=2
    Case "IntCircut" 'replace this with the word represending "IC"
    sPreFix = "IC"
    X=3
    End Select

    Mysql = "SELECT Max(CDbl(Mid([YouFieldName]," & X & "))) AS NewNum
    FROM tbl_YourTblName Where YourFieldName Like " & sPrefix & "*"

    'open a recordset and get the max number for the group
    Set conn = CurrentProject.Connection
    rs.Open Mysql, conn, adOpenKeyset, adLockOptimistic
    If not rs.EOF then
    sSuffix = rs!NewNum
    End If
    Set rs=nothing

    'combines the sPrefix and sSuffix to create your new key
    sKey = sPrefix & sSuffix +1

    'assign the sKey value to your text box
    Me.YourFieldName = sKey

  3. #3
    whitelockben is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    5
    What a great answer, thank you very much for your help!!

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

Similar Threads

  1. Replies: 8
    Last Post: 05-26-2011, 09:23 AM
  2. Linking Combobox back to table
    By jlclark4 in forum Forms
    Replies: 52
    Last Post: 12-31-2010, 03:18 PM
  3. Replies: 9
    Last Post: 12-18-2010, 12:51 PM
  4. Replies: 7
    Last Post: 12-15-2010, 09:46 AM
  5. Page break on report inserts empty page between
    By Galadrielle in forum Reports
    Replies: 0
    Last Post: 07-07-2010, 04:18 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