none
[Access 2003+VBA] How to lock combobox when value has been selected RRS feed

  • Question

  • Hi,

    I tried this code to lock a bound combobox when we choose a value:

    'Security lock for comboBox
      If Not IsNull(Me![comboBox]) Or Me![comboBox] = "" Then
        comboBox.Locked = True
      Else
        comboBox.Locked = False
      End If
    
    
    

    but, it's not working?!

    How to fix it, please?

    Thanks

    Saturday, August 27, 2011 4:35 PM

Answers

  • if combobox is bind with a database table field, it should be sufficient the event on_current. If it doesn't work 'immediately', there's something else to verify and we can't know what.

    Another thing: Me.combobox.Text works only if the combobox has focus.

    In on_current event handler, you should have to use Me.combobox.Value (and this must be a string), but - for a more sure result, you have to verify the field.

    Let me suppose the field is IdCategory. You have a combobox bind on this field, and its name is IdCategory, and the rowsource is a table Categories with fields IdCategory (long) and Category (text), so you set the combo with two columns, with 0 and 5 cm widths, boundcolumn being the first.

    In oncurrent event handler, you have to verify simply if you are on a new record (the value of IdCategory is null till you fill any other field) and you can verify simply with

    If me.NewRecord Then

    ' any setting, and

    combobox.locked = false

    else

    combobox.locked = true

    End If

    But if boundcolumn is the second (the Category field, that is string), and the combobox name is Category, you have to verify

    If Not (IsNull(Me.Category) Or Me.Category = "") Then

    or, if you prefer,

    If Not nz(Me.Category, "") = "" then

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by iboumiza Friday, September 9, 2011 11:43 PM
    Wednesday, August 31, 2011 6:49 AM

All replies

  • first, Me!Combobox cn't be null: it's an object.

    You have to verify Text property of the control, not the value of the field.

    Second, you have to include both the 'not' valid conditions into a parenthesis.

    Third, you must think the controls, not the fields

    So, this is the correct code:

    combobox.locked = Not (IsNull(Me.combobox.Text) or Me.combobox.text = "")

    then the user can open the list of the combobox, but can't modify the value clicking on a different element fo the list.


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Saturday, August 27, 2011 6:07 PM
  • ok, I tried it on on_click, on_open and on_current events and it works after changing the value in the combobox!

    what event or change do I have to do/choose so it can be by default ?

    Tuesday, August 30, 2011 8:47 PM
  • You want it to be blocked by default? That would mean on the form_load event, if I get it right.

    Regards,

    Dylan Meeus


    0x2B |~ 0x2B Blog : www.it-ca.net/blogdylan
    Wednesday, August 31, 2011 2:26 AM
  • if combobox is bind with a database table field, it should be sufficient the event on_current. If it doesn't work 'immediately', there's something else to verify and we can't know what.

    Another thing: Me.combobox.Text works only if the combobox has focus.

    In on_current event handler, you should have to use Me.combobox.Value (and this must be a string), but - for a more sure result, you have to verify the field.

    Let me suppose the field is IdCategory. You have a combobox bind on this field, and its name is IdCategory, and the rowsource is a table Categories with fields IdCategory (long) and Category (text), so you set the combo with two columns, with 0 and 5 cm widths, boundcolumn being the first.

    In oncurrent event handler, you have to verify simply if you are on a new record (the value of IdCategory is null till you fill any other field) and you can verify simply with

    If me.NewRecord Then

    ' any setting, and

    combobox.locked = false

    else

    combobox.locked = true

    End If

    But if boundcolumn is the second (the Category field, that is string), and the combobox name is Category, you have to verify

    If Not (IsNull(Me.Category) Or Me.Category = "") Then

    or, if you prefer,

    If Not nz(Me.Category, "") = "" then

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by iboumiza Friday, September 9, 2011 11:43 PM
    Wednesday, August 31, 2011 6:49 AM
  • If me.NewRecord Then
    
    ' any setting, and
    
    combobox.locked = false
    
    else
    
    combobox.locked = true
    
    End If
    
    
    


    If good!!!

    Thank you !!!

    Friday, September 9, 2011 11:43 PM