Link to home
Start Free TrialLog in
Avatar of TommyN14
TommyN14

asked on

Create textboxes at run time with access xp...

Hi,

Let's start with an example:

This is my table layout:
table name:  tblStudent
StudentID     FirstName      LastName     MI     Gender
----------------------------------------------------------
1                  Johnny          A                  N       M
2                  Jen               B                  J        F
3                  Tom             C                  K       M
...

On the form, this is the format i want to be displayed:

Student 1(label)          Student 2(label)          Student 3(label)
Johnny (textbox)        Jen (textbox)              Tom (textbox)
A          (textbox)        B    (textbox)              C     (textbox)
N          (textbox)        J    (textbox)              K      (textbox)
M         (textbox)        F    (textbox)              M     (textbox)

where textbox is a TextBox control and label is a Label control.

Here is my question:
How do i create these textboxes dynamically at run time and based on how many students i have in the table to create that many columns on the form?  Example, if i have 2 more records in the table, i'd have two more columns namely Student 4 and Student 5 on the form.

Thanks!
Avatar of jadedata
jadedata
Flag of United States of America image

You have to be very concerned with creating controls dynamically on forms if the content of the data is not stable.  For example:  What will the maximumn number of students be when performing this operation?
You'd be better off setting up the form to take the maximum number of students and hiding (.visible = False) the ones you don't need.
You forms will thank you for it.  And they will load faster based on the fixed format.
Hi TommyN14

Bit of info here:
Title: Create controls in runtime on form or report
https://www.experts-exchange.com/questions/10052373/Create-controls-in-runtime-on-form-or-report.html

Fair comment from jadedata.

Regards Alan
Avatar of kaaviyam
kaaviyam

To overcome the problems of having more no. of students, better to fix the no. of columns and create all the required controls at runtime and hide/display them according to the no. of students.
Avatar of TommyN14

ASKER

Hi,

I do understand and aware of the problem of having more students later on.  And in my program i'll have a condition checking for the max no. of students.  But for now, let's say the max no of students to be displayed at a time 10, and if more than that, problably displays a navigations.  I don't want to create 10 columns at design time and invisible them if the no. of students is < 10.  This will make the form look bad (a lot of empty spaces), especially this is a subform.

Thanks!
can you pls. tell the big picture, may be we can have a different view of the issue. What does the main form contain, why is the subform arranged horizotally other than vertical. may be there is a different way to get the result you want.
Could the students simply be "listed" in a continuous subform that can take edits instead?  This would be a much more dynamic way of showing a filtered list of students than creating controls on the fly.  The subform would only show what's needed and there is no special coding required to make it happen.
1.  The reason i want to display data horizonally because at the next step, i'm gonna implement the part where i can allow the user to move columns around so they can view the most important columns (students) on the far left  of the screen and also display all the data per student on a form instead of scrolling left/right to view the entire data.  Remember, the above data is just examples.  In reality, i have a lot more info per each column.

2.  Listing students in a continuous subform only show one student at a time.  If we want to view the next student, we'd have to click on the next button in the navigation control.  I don't like this design.  I want to be able to list the max of 10 students at a time on a screen.  It's true that when we list 10 students at a time, the screen is not big enough to displays all 10.  So we will have to scroll to the left to view more students.  That's why i want to be able to move columns around so the important columns(students) will be on the left and less important columns will be on the far right.
Partner you are painting yourself into a very deep corner here.  The process you describe is going to take you months to get under control at your current pace.  There is not enough space in this memo box to begin to list the challenges you are making for yourself.

Your subform list can be a few select field from the table that conclusively identify students.  The records can be just a row of text boxes with that info.  The list can be filtered to exclude unwanted records.  The Form_Current event can be the trigger to update the main form "Details" fields with all the additional details about the student.

All this control create and move stuff is gonna require more code than I can tell you.

Keep it simple...
Any1 else have any ideas?
If your importance was based on any specific column(s) value then you could 'sort' them out, and put the important ones at the top.

To my opinion it is always best to have rows arranged one by one vertically, unless until it is a crosstab query, ie you calculate values on both vertically and horizontally.

But in this case I couldn't figure out, how the user will move the columns left/right.

MSAccess deals with each row individually to display them, if your process is also row based it is easy for you.

Sorry, I couldn't help you.
Hi kaaviyam,

I got the part where the data can be shifting around already.  Basically, i created an array holding the max no. of students and sort them then reload it to the form.  When the user click on the Save button, it will update to the correct record.

I just need to know how to create the textboxes on the fly.  Thanks for trying though.

TommyN14
I don't know exactly how to do that. But still see if this can help you.

As I mentioned before create the required no. of controls in design view, but with width -very small number and left common for all unused controls, so that they lay in one location and make them visible false. ie is to say you have only one set of controls visible and the others are not, as well the form size is not too wide, to have just empty space.

And as and when required make the required controls visible, wider and moved right. You may have a local procedure to do this.

I don't know exactly how to do that. But still see if this can help you.

As I mentioned before create the required no. of controls in design view, but with width -very small number and left common for all unused controls, so that they lay in one location and make them visible false. ie is to say you have only one set of controls visible and the others are not, as well the form size is not too wide, to have just empty space.

And as and when required make the required controls visible, wider and moved right. You may have a local procedure to do this.

Hi TommyN14,

RE: I just need to know how to create the textboxes on the fly.

This code creates a new form adds a textbox and a label for student Johnny.

Private Sub Command0_Click()
 
  Dim MyForm As Form, MyControl As Control, sFormname As String
  Set MyForm = CreateForm()
  sFormname = MyForm.Name
  Set MyControl = CreateControl(MyForm.Name, acTextBox, acDetail)
 
  With MyControl
    .Width = 1500
    .Height = 200
    .Top = 440
    .Left = 1600
    .Name = "txtJohnny"
  End With
 
  Set MyControl = CreateControl(MyForm.Name, acLabel, acDetail)
  With MyControl
    .Width = 1500
    .Height = 200
    .Top = 440
    .Left = 0
    .Name = "lblJohnny"
    .Caption = "Johnny"
  End With
 
  DoCmd.Close acForm, MyForm.Name, acSaveYes
  DoCmd.OpenForm sFormname, acNormal
 

End Sub


Regards Alan
Alan,

Can this be done within the same form instead of open as a new form?  
I want to be able to when open the form, it goes to the table and grap all the data in the table and iterate though each record and create the textboxes on the form and display the data for each field in each record.  Another preferable way is to have a subform does all this and when i open the "Main" form which contain that subform, i'll be able to see all the textboxes based on the number of fields/records in the table.

I hope this is clear.  If not please let me know.

Thanks!

TommyN14
Hi TommyN14,

I understand what you want, but I believe that controls can only be added to forms in design view, and since you cant access design view while you are executing a code module on the form that you want to make design changes on, I dont think this be done within the same form.

You may be able to make the changes on a form using the above sample then attach the new form as a sub-form of the current form programatically. I'm a little pressed for time at the moment, but may get a chance later to try it. or you could try it and let me know how you get on.

Regards Alan

ASKER CERTIFIED SOLUTION
Avatar of Alan Warren
Alan Warren
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, Alan