?
Solved

Adding controls dynamically...

Posted on 2003-03-19
4
Medium Priority
?
151 Views
Last Modified: 2010-04-07
Hi,

In my application iam generating a layout with label controls in the form onload event. In the onload event it will read the properties like X-position, Y-position and name from the database table. I want to delete the control and from the layout and to update the database table. If i tried to delete the control using Form1.Cotrols.Remove "CtrlName"  Its giving "Controls.Remove can only remove controls added with Controls.Add".

Here is my code:

Private Sub Form_Load()

Dim TabCount As Integer
Dim objCmd As New ADODB.Command
Dim objRec As New ADODB.Recordset

objCmd.ActiveConnection = "dsn=Floordsn"
objCmd.CommandText = "sp_FloorItems_FetchAll"
objCmd.CommandType = adCmdStoredProc

objRec.Open objCmd
 intIndex = 1
While Not objRec.EOF
   
    Call Controls.Add("VB.Label", "Label0" & intIndex)
    Load Label0(Label0.Count)
    With Label0(intIndex)
            .Visible = True
            .Width = 800
            .Top = objRec("FL_PosY")
            .Left = objRec("FL_PosX")
            .Height = 195
            .Caption = objRec("FL_Type")
        End With
    intIndex = intIndex + 1
   
    objRec.MoveNext
Wend


'Close the recordset and delete the objects
objRec.Close

Set objRec = Nothing
Set objCmd = Nothing
End Sub

Private Sub Label0_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
       
        mnuSub(0).Visible = True
        While mnuSub.UBound > 0
            Unload mnuSub(mnuSub.UBound)
        Wend

        'Load menu items
        Load mnuSub(1): mnuSub(1).Caption = "Delete": mnuSub(1).Tag = Index
       
        mnuSub(0).Visible = False
        PopupMenu mnuFile
    End If
End Sub

Private Sub mnuSub_Click(Index As Integer)
   
   Form1.Controls.Remove Label0(2)
End Sub
0
Comment
Question by:CHSrinivas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 8165987
Hi CHSrinivas,

You need to use the UNLOAD statement instead:

Private Sub mnuSub_Click(Index As Integer)
   
   Unload Label0(2)
End Sub

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 

Author Comment

by:CHSrinivas
ID: 8166036
Hi TimCottee

Thank you for your solution. Its working fine.

Cheers
Srinivas
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 225 total points
ID: 8166087
Please accept my comment as an answer to close out this thread.
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8166459
Can I just mention

Call Controls.Add("VB.Label", "Label0" & intIndex)
Load Label0(Label0.Count)

Creating label01,label02,label03 etc.
Then the next statement is lading alabel in the control array Label0.

I think first off you should remove this line as it is doing nothing it is not referenced in your code.

Call Controls.Add("VB.Label", "Label0" & intIndex)

and as TimCottee stated unload the control array elements as folows.

Unload Label0(SomeIndex)

If you had intended to use

Call Controls.Add("VB.Label", "Label0" & intIndex)

you would then need to call

Call Controls.Remove "Label0" & intIndex


Regards


John McCann



   
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question