Avatar billede AnyFellow Mester
09. juni 2016 - 14:29 Der er 21 kommentarer og
2 løsninger

Oprette userform dynamisk

Jeg har en skabelon med en userform, der åbner når brugeren har åbnet skabelonen.

Denne userform indeholder en meget stor mængde felter, afkrydsningsbokse osv.

Når der skal tilføjes en ny del, skal den ofte ind mellem de eksisterende elementer, hvilket tager lang tid idet alt gerne skal stå perfekt linet op.

Er det muligt at programmere sig ud af oprettelsen af en sådan userform?

Altså, kan jeg i VBA åbne en userform for brugeren, og så derfra fylde bokse, afkrydsningsbokse osv. i den?
Avatar billede terry Ekspert
09. juni 2016 - 14:43 #1
Not sure what your after!

If you already have a form containing fields then you will need to manually place the fields where you want them, using code wont be able to decide that for you.

If you create a new form using the form wizard then Access will place them on the form lined up but not necessarily in the order you want them or where you want them.
Avatar billede terry Ekspert
09. juni 2016 - 14:47 #2
Here's some code which allows you to add fields to a form.
https://msdn.microsoft.com/en-us/library/office/aa221167(v=office.11).aspx
Avatar billede AnyFellow Mester
10. juni 2016 - 12:00 #3
Fik ikke skrevet at skabelonen er lavet i Word.

Når brugerne går ind i skabelonen, åbnes en userform, der lader dem krydse forskellige felter af samt skrive i andre felter. På baggrund af disse felter dannes et brev.

Det er userformen jeg ønsker at danne automatisk, ikke noget i mit word-dokument, idet jeg allerede har styr på den del.
Avatar billede supertekst Ekspert
10. juni 2016 - 12:21 #4
Det kan godt lade sig gøre at programmere en Userform - har gjort det for år tilbage. Udført via VBA.
Avatar billede AnyFellow Mester
10. juni 2016 - 13:00 #5
Har du nogle kodeeksempler eller et link?
Avatar billede supertekst Ekspert
10. juni 2016 - 14:34 #6
Prøver at finde eksempler på det jeg har  fabrikeret engang.
Avatar billede supertekst Ekspert
10. juni 2016 - 15:10 #7
Prøv at beskrive i detaljer hvad du tænker på i forbindelse med automatisering af en Userformens skabelse.
Hvad er grundlaget?
Avatar billede AnyFellow Mester
10. juni 2016 - 23:19 #8
Et eksempel kunne være, at jeg har en userform med 50 checkboxe. Nu skal der tilføjes en i midten et sted.

Word editor tillader jeg kan flytte en masse checkboxe med musen, men så bliver placeringen kun på øjemål, ellers skal de flyttes ned enkeltvis.

Mit foretrækker at placeringer er nøjagtig, hvilket leder mig til at gerne ville kunne danne userformen med indhold dynamisk.
Avatar billede terry Ekspert
13. juni 2016 - 09:20 #9
As I mentioned in my first comment, I dont see that using code is going to help much.

The code can not decide where among all the other fields the new fields is to be placed, that had to be a manual process. You could of course alter the code (manually) ever time you want to make a design alteration, but that's not going to save time.

You could have all the forms parameters in a table (position, field type, bound field .....and many others)
And then make code which loops through records to create the form.
But there is no simple solution
Avatar billede AnyFellow Mester
13. juni 2016 - 21:11 #10
Terry...> I can see a lot of ways it would be easyer.

It would be easier to change the position in my code, because i only have to change the value one time on each object. If i have to move 50 boxes in the design view, i have to click on each box, and the type the new top-value.

I would assume it would be possible to calculate the top-value for each box, because they are placed with precise the same space.
Avatar billede terry Ekspert
14. juni 2016 - 08:45 #11
"If i have to move 50 boxes in the design view, i have to click on each box, and the type the new top-value."

That's not correct, you can select one or more and move them together. You can also select all of them (Ctrl+A) at once and move them all together if need be.

Lets say you have 20 fields on a form all nicely spaced above each other. How do you tell the code which ones need to be moved to make room for another new field?

Its not a problem, takes a bit of time though, to make code which can read the coordinates of controls and if necessary move them around, but you still need some way of telling the code where to move them and which ones are to be moved.
Avatar billede terry Ekspert
14. juni 2016 - 09:11 #12
There are built in features in Acces which can help you align controls etc.
And you can find videos on YT showing how to use them.
EG:
https://www.youtube.com/watch?v=ATI5vcRA728
Avatar billede AnyFellow Mester
15. juni 2016 - 07:57 #13
Terry...> I know i can mark all boxes and move them at once, but they have to be positioned with the mouse. It is not very easy to position them accurate.

I do not use Acess. I use Word.
Avatar billede terry Ekspert
15. juni 2016 - 10:02 #14
In my comments I have just focused on controls on an Access form where using VBA it could be possible to move them about, and I also mentioned the ups and downs of using code for doing that.

If you want to move the controls on a word template from Access then things get a little more difficult because you need to automate Word to do this. Basically it means you start Word in the background and use that to make the design changes in the document/template.

You can find loads of information on the web on automation EG.
https://support.microsoft.com/en-us/kb/313193
Avatar billede terry Ekspert
15. juni 2016 - 10:05 #15
To be honest, unless you are making changes to the template all the time then trying to make a system which can do this for you is going to take a long time.
Avatar billede AnyFellow Mester
15. juni 2016 - 18:01 #16
I do not know why you are mixing Access into this.

My project is in Word alone.

I was asking for: how to add a control to a userform?

Its a fairly small task for one who knows how to do it.
When i know how this is done, i can see for myself if it would be the right solution.
Avatar billede terry Ekspert
15. juni 2016 - 18:50 #17
"I do not know why you are mixing Access into this." :-)
Not sure why we are either, for some silly reason I thought we were in category Access, sorry about that.

"Its a fairly small task for one who knows how to do it."
I still dont think it is. VBA code still doesn't know where you want to move the existing fields from/to, and which ones.
The code to move  controls is simple, but the task of how to tell the code which ones to move isn't so simple. Or at least I dont think so.

Yes, please get back with your solution so we all can learn from it.
Avatar billede AnyFellow Mester
15. juni 2016 - 22:10 #18
The problem is not to move them.

I am looking for code on how to add fx a checkbox.
Avatar billede terry Ekspert
16. juni 2016 - 09:04 #19
"Når der skal tilføjes en ny del, skal den ofte ind mellem de eksisterende elementer, hvilket tager lang tid idet alt gerne skal stå perfekt linet op."

So you dont need to move them?

This may be of help
http://www.tushar-mehta.com/publish_train/xl_vba_cases/1051%20Add%20userform%20items%20on%20the%20fly.shtml
Avatar billede AnyFellow Mester
16. juni 2016 - 11:50 #20
I will have a look at the link.

Example:
I have cover letter made as a template in Word. The userform has 50 different checkboxes in a particulier order.

Everytime the template is opened, the template should open with a empty userform, and then add the 50 different checkboxes in order specified in the programming code.

If i have to add a checkbox between the other checkboxes in the template, i can do that by adding the checkbox in the programming code between the other checkboxes.

When the userform has all checkboxes added, i do not need to move them around.
Avatar billede terry Ekspert
16. juni 2016 - 12:10 #21
Well I think you have the code to create one control, so its just a case of adding code for the remaining controls.
Avatar billede AnyFellow Mester
16. juni 2016 - 15:12 #22
Et lille eksempel på hvad jeg ville kan ses nedenfor.

Indsættelse af 3 checkboxe:

Private Sub UserForm_Initialize()
    Dim placeringTop, placeringLeft As Integer
      placeringTop = 20
      placeringLeft = 20
   
    Set addElement1 = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox1")
    With addElement1
    .Width = 500
    .Left = placeringLeft
    .Top = placeringTop
    .Caption = "Dette er en test 1"
    End With
   
    Set addElement2 = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox2")
    With addElement2
    .Width = 500
    .Left = placeringLeft
    .Top = addElement1 + addElement1.Top + 2
    .Caption = "Dette er en test 2"
    End With
   
    Set addElement3 = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox3")
    With addElement3
    .Width = 500
    .Left = placeringLeft
    .Top = addElement2 + addElement2.Top + 2
    .Caption = "Dette er en test 3"
    End With
End Sub


Indsættelse af 1 ekstra checkbox:

Private Sub UserForm_Initialize()
    Dim placeringTop, placeringLeft As Integer
      placeringTop = 20
      placeringLeft = 20
   
    Set addElement1 = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox1")
    With addElement1
    .Width = 500
    .Left = placeringLeft
    .Top = placeringTop
    .Caption = "Dette er en test 1"
    End With
   
    Set addElement1a = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox1a")
    With addElement1a
    .Width = 500
    .Left = placeringLeft
    .Top = addElement1 + addElement1.Top + 2
    .Caption = "Dette er en test 1a"
    End With

  Set addElement2 = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox2")
    With addElement2
    .Width = 500
    .Left = placeringLeft
    .Top = addElement1a + addElement1a.Top + 2
    .Caption = "Dette er en test 2"
    End With
   
    Set addElement3 = Userform_Folgebrev.Controls.Add("forms.checkbox.1", "checkbox3")
    With addElement3
    .Width = 500
    .Left = placeringLeft
    .Top = addElement2 + addElement2.Top + 2
    .Caption = "Dette er en test 3"
    End With
End Sub
Avatar billede terry Ekspert
16. juni 2016 - 15:25 #23
Good you get there in the end
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester