Skip navigation

How to addcontrol in form of another mdb
This function is very simple for insert a command button with Event Procedure
in form any mdb (microsoft access)
This function coding by VBA and can work in VB
Please remember this library must be import to project first
“Microsoft Access 10.0 Libraries” or heigher

Function AddCtl(dbPathAndName As String, frmName As String, OName As String, NName As String)
Dim acc As Access.Application ‘ Destination of microsoft access database (mdb)
Dim frm As Form ‘ object form variable
Dim ctlO As Control ‘ object old control in that form variable (static point to reference by a new control)
Dim ctlN As Control ‘ object new that you will make variable
Dim mdl As Module ‘ object module for place Event Procedure of new control
Dim mdlLine As Long ‘ variable for count of line in module that you start insert a new procedure

‘****************** connect to that database and open destination form in design view ************
Set acc = CreateObject(“access.Application”)
On Error GoTo ExitErr
acc.OpenCurrentDatabase (dbPathAndName)
acc.DoCmd.OpenForm frmName, acDesign

Set frm = acc.Forms(frmmane) ‘ set variables for object form
Set mdl = frm.Module ‘ set variables for object module
Set ctlO = frm.Controls(OName) ‘ set valibles for reference control

‘ create a new control in form, place it in front of old control (or somewhere by reference to old control)
‘ and set it in to new control variable

Set ctlN = acc.CreateControl(frm.Name, acCommandButton, _
acDetail, , , ctlO.Left – (ctlO.Width + 50), ctlO.Top, ctlO.Width, ctlO.Height)

‘ set original properties for new control
ctlN.Name = NName
ctlN.Caption = “Hello!”
ctlN.OnClick = “[Event Procedure]”

‘ create a code “Procedure fon OnClick Event” etc., by “CreateEventProc command
‘ in this step VBA will create start line in some line of module, But we can get a number of line
‘ by itself
mdlLine = mdl.CreateEventProc(“Click”, ctlN.Name)

‘ and the next line from above is a start line of you command
mdl.InsertLines mdlLine + 1, “msgbox ” & Chr(34) & “Hello” & Chr(34)
‘ in anotherway you can in sert a code pattern form a file by method of module
‘ ex. mdl.addfromfile

‘besure close any object
acc.DoCmd.Close acModule, mdl.name , acSaveYes
acc.DoCmd.Close acForm, frm.Name, acSaveYes

Exitt:
acc.CloseCurrentDatabase
Set acc = Nothing
Exit Function

ExitErr:
MsgBox Err.Description
Resume Exitt
End Function

*//one sample for use the function like this : \\*
*\\send values to every variable in function and run //*

Sub runn()

Dim dbPth, frmN, OldN, NewN As String

dbPth = CurrentProject.Path & “\mdbform.mdb”
frmN = “frmtest”
OldN = “cmdExit”
NewN = “cmdTest”

call AddCtl(CStr(dbPth), CStr(frmN), CStr(OldN), CStr(NewN))

End Sub
………………………Gentleman Yeadram ……………………

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

%d bloggers like this: