Results 1 to 2 of 2
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Renaming a control at Runtime (VB6 SP5)

    Hi,

    I have a function which loops round all of the controls on a form. Where the control name matches a field in a table, some data gets loaded as the controls combo dropdown list.
    This has always worked fine, however, a problem has recently reared its head:

    Adding new records doesnt need to display contacts that have left the company in the dropdown, however, viewinghistoric data does!

    I wrote the original function to be dynamic i.e. if control.name = rec.fields("controlname") then sql = rec.fields("combosql")
    Therefore, the easiest way for me to get round this issue would be to rename the control.

    I'd have 2 records in the database:
    controlname |combosql
    ContactsWithClosed |select contact from tblcontacts where estabID = %var1%
    ContactsWithoutClosed | select contact from tblcontacts where estabID = %var1% and contactType <> 'Closed'

    If I rename the control ContactID to ContactsWithClosed the first bit of line of sql gets used, if I rename it to ContactsWithoutClosed the second line of sql gets used.

    Is Control renaming at runtime possible or will I be needing to make some drastic changes to my simple dynamic function?

    Regards,

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming a control at Runtime (VB6 SP5)

    I approached it from a different angle and solved the problem - i'll leave it here in case anyone else has the same issue.

    'Im using the it-partners ITCombo control
    Dim WithEvents Cmd1 As itgrid6.itCombo

    Public sub Form_Load()
    Dim ComboName As String
    'Set what the combo control will be called, if a primarykeyid hasnt been set overwrite Comboname to select the alternate sql
    ComboName = "ContactsWith"
    If PrimaryKeyID = Empty Or IsNull(PrimaryKeyID ) Then ComboName = "ContactsWithout"

    'Add a new control to the form called either ContactsWith or ContactsWithout depending what SQL is needed
    Set Cmd1 = Controls.Add("itgrid6.itcombo", ComboName)
    'Clone the existing control over the new one - this means the newly created control has the correct font etc
    Cmd1.Clone ContactId 'This makes sure the new combo takes the font properties of the contactid combo
    'This is my function that populates the combos. It loops all of the controls on the form and loads the dropdown list for any that match.
    Call PopulateFormComboBoxes(Me.Name, Me)
    'Clone the new control over the original control - this also clones the newly populated dropdown list.
    ContactId.Clone Cmd1
    'remove the extra control so it isn't picked up in the save function
    Controls.Remove Cmd1.Name
    Set Cmd1 = Nothing 'Clean up variable
    end sub

    That got me my combo that shows "deleted" contacts who have left the company if opening an existing record, but only shows active contacts if adding a new record.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •