Skip navigation

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 higher

Function AddCtl(dbPathAndName As String,  frmName As String, _
  OName As String,  NName As String) 

' Destination of microsoft access database (mdb)
' object form variable
' object old control in that form variable _
'    (static point to reference by a new control)
' object new that you will make variable
' object module for place Event Procedure of new control
' variable for count of line in module _
'    that you start insert a new procedure 

Dim acc As Access.Application
Dim frm As Form
Dim ctlO As Control
Dim ctlN As Control     
Dim mdl As Module      
Dim mdlLine As Long   
'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 variables for object form
' set variables for object module
' set valibles for reference control

Set frm = acc.Forms(frmmane)
Set mdl = frm.Module
Set ctlO = frm.Controls(OName)
' 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,  ,  acSaveYes 
acc.DoCmd.Close acForm,  frm.Name,  acSaveYes 
Set acc = Nothing 
Exit Function 
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

One Comment

  1. Your style is so unique compared to other
    folks I have read stuff from. I appreciate you for posting when you’ve got the opportunity, Guess I will just book
    mark this site.


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

You are commenting using your 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: