Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic form: creating button/control at run-time 5

Status
Not open for further replies.

KerenPisan

Programmer
Feb 27, 2004
2
US
Hi all,
Does anyone know if ACCESS 2000 could build dynamic form? For example, once we click one button then it will create and display another button.

Thanks for all the help.
 
Hi,

Responding a little late - and not sure exactly what you are up to, but you can build controls and even forms dynamically from code.

Some of the bits and pieces here might be useful.

Below is code that builds a simple form and puts code in a module for that form so the form appears with working VBA. It gets created only from this code - then vanishes unsaved when closed.

Best,
C


' ' ' ' ' code start ' ' ' ' '
Private Sub NewControls()
Dim frm As Form
Dim mdl As Module
Dim ctlLabel As Control
Dim ctlText As Control
Dim ctlText2 As Control
Dim BtnTest As Control
Dim a As String
Dim b As String
Dim strAddCode As String
Dim StrSQL As String
Set frm = CreateForm ' Create new form
a = frm.Name
StrSQL = ""
StrSQL = StrSQL & "SELECT TblContacts.ContactID, TblContacts.FirstName, TblContacts.LastName"
StrSQL = StrSQL & " FROM TblContacts"
StrSQL = StrSQL & " WHERE (((TblContacts.ContactID) = [Forms]![FrmSmallFoot002]![contactID]))"
StrSQL = StrSQL & " WITH OWNERACCESS OPTION;"
frm.RecordSource = StrSQL
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "FirstName", 2300, 100, 3000)
With ctlText
.TabIndex = 0
End With
' hmmm...width of label seems to be dependent on text within
Set ctlLabel = CreateControl(frm.Name, acLabel, , ctlText.Name, "First Name", 1300, 100, 1000)
With frm
.DividingLines = False
.ScrollBars = False
.RecordSelectors = False
.AutoResize = True
.AutoCenter = True
.BorderStyle = 3 ' 0-borderless,1-thin,2-sizable,3-dialog
.Caption = "Contact for " & Forms![FrmSmallFoot002]![CompanyName]
.Modal = True
.ControlBox = False
.HasModule = True
.NavigationButtons = False
.Cycle = 1 ' 0-AllRecords, 1-CurrentRecord
.KeyPreview = True
End With
Set BtnTest = CreateControl(frm.Name, acCommandButton, , , "", 300, 150, 600, 600)
b = BtnTest.Name ' KEEP c is used in code to close form
Set ctlText2 = CreateControl(frm.Name, acTextBox, , "", "LastName", 2300, 500, 3000)
With ctlText2
.TextAlign = 1 ' left align
.TabIndex = 1
End With
Set ctlLabel2 = CreateControl(frm.Name, acLabel, , ctlText2.Name, "Last Name", 1300, 500, 3000)
BtnTest.OnClick = "[Event Procedure]"
Set mdl = Forms![Form1].Module ' refer to the module
strAddCode = "" ' start to build code
' add code below to close form w/o saving it
strAddCode = "Private Sub " & b & "_Click()" & vbCrLf
strAddCode = strAddCode & "DoCmd.Close acForm, me.name, acSaveNo"
strAddCode = strAddCode & vbCrLf
strAddCode = strAddCode & "End Sub" & vbCrLf
strAddCode = strAddCode & vbCrLf
' add code to keep it on one record despite page up pagedown keystrokes
strAddCode = strAddCode & "Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)"
strAddCode = strAddCode & vbCrLf & "Select Case KeyCode"
strAddCode = strAddCode & vbCrLf & "Case 33, 34, 18"
strAddCode = strAddCode & vbCrLf & "KeyCode = 0"
strAddCode = strAddCode & vbCrLf & "Case Else"
strAddCode = strAddCode & vbCrLf & "End Select"
strAddCode = strAddCode & vbCrLf & "End Sub"
With BtnTest ' describe button
.Caption = "&Close"
.Cancel = True
.TabIndex = 2
End With
With mdl ' add code to module here
.InsertText strAddCode
End With
DoCmd.OpenForm a, acNormal ' open form to normal view from design view
DoCmd.MoveSize , , 5700, 1400 ' set size
End Sub
' ' ' ' ' code end ' ' ' ' ' '
 
Awesome dude...you helped me. Exactly what I needed.
Thank you thank you thank you :)

Sera
I often believe that...
My computer is possessed!
 
Ohh I guess I should add that....

the only trouble I had with this code was the fact that cgarts declares his sub as PRIVATE. I added this code into a module and then tried to call it from another form....I kept getting the error No Procedure defined...Then I figured out I needed to declare the sub as PUBLIC. This solved it. This may help some of you who are trying to get this code to work. If I was adding this code to a "class module" that was calling the code then it would be appropriate to declare the sub as PRIVATE. If I wanna call the function from anywhere...anyhow...it better be declared as PUBLIC.



Sera
I often believe that...
My computer is possessed!
 
Sorry for the late thank-you, but it helped. Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top