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!

Modifying the default Access popup dialog box 5

Status
Not open for further replies.
Oct 24, 2002
512
US
Does anybody know how to change the default dialog box? I know how to create dialog boxes from scratch but sometimes it seems like it would be quicker to just change some stuff on the default message box. I simply don't have a clue how to do it. Any ideas?

Thanks!
Ann
 
Andy/Michael, WOW!

My VBA experience is limited so I'll be a while completly absorbing Andy's code. I didn't even know you could create objects "on the fly". I'm really excited now. A whole new area to explore.

An example of my inexperience: When I run the module the form opens up maximized and I don't know how to programmatically resize a form.

Thanks for jumping onto this thread. Especially, Andy -- Thanks for your hard work. Here's a star for you!

Ann
 
Ann, Michael & EvilCabal

Ok, Ok I was being lazy!!! Michael is right that thinnggyyyy was not saved, was not therefore registered as a form and was therefore an orphan and cannot be modal to anything.

So... following on from that here is the latest code.. it works but can be messy!!!

DialogBox.bas module
--------------------
Option Compare Database
Option Explicit
Public Type dlgControlRec
CtrlType As Integer
CtrlCaption As String
CtrlLabel As String
CtrlAction As String
End Type
Public Type dlgControls
Ctrl(10) As dlgControlRec
End Type
Public Type dlgRetValRec
ButtonPress As Integer
CtrlValue As String
End Type
Public Type dlgRetVals
ReturnCtrl(10) As dlgRetValRec
End Type

Public DlgRetVal As dlgRetVals
Dim frm As Form
Dim ctl As Control
Dim mdl As Module
Dim lngReturn As Long
Function dBox(strCaption As String, dlgctrls As dlgControls) As String
Dim oldname As String
Dim i As Integer

On Error GoTo Error_dBox
'Delete old form - removing all previous controls etc
DoCmd.DeleteObject acForm, "¿DialogBox¿"

'Create new DialogBox
Set frm = CreateForm
oldname = frm.NAME
DoCmd.Close acForm, frm.NAME, acSaveYes
DoCmd.Rename "¿DialogBox¿", acForm, oldname

'Open Form
DoCmd.OpenForm "¿DialogBox¿", acDesign

' Set properties to form.
Set frm = Forms!¿DialogBox¿
frm.Caption = strCaption
frm.DefaultView = 0 'Single Form
frm.ScrollBars = 0 'Neither
frm.Visible = True
frm.Modal = True
frm.NavigationButtons = False
frm.RecordSelectors = False

i = 1
While dlgctrls.Ctrl(i).CtrlType <> 0
DoCreateControls dlgctrls, i
i = i + 1
Wend

RealignControls
DoCmd.Close acForm, frm.NAME, acSaveYes
DoCmd.OpenForm &quot;¿DialogBox¿&quot;, acNormal, , , , acDialog

Exit_dBox:
Exit Function

Error_dBox:
If Err.Number = 3011 Then
'First Time of run? Form did not exist to be deleted
'Continue normally to create
Resume Next
Else
MsgBox Err & &quot; :&quot; & Err.DESCRIPTION
DoCmd.Close acForm, frm.NAME, acSaveNo
Resume Exit_dBox
End If
End Function
Private Sub DoCreateControls(dlgctrls As dlgControls, intControl As Integer)
Set ctl = CreateControl(frm.NAME, dlgctrls.Ctrl(intControl).CtrlType, , , , 0, 0)
If dlgctrls.Ctrl(intControl).CtrlLabel <> &quot;&quot; Then
Set ctl = CreateControl(frm.NAME, acLabel, , , , 500, 500)
ctl.Caption = dlgctrls.Ctrl(intControl).CtrlLabel
ctl.Width = Len(dlgctrls.Ctrl(intControl).CtrlLabel) * (ctl.FontSize * 15)
ctl.Height = 225
End If

If dlgctrls.Ctrl(intControl).CtrlType = acCommandButton Then
ctl.Caption = dlgctrls.Ctrl(intControl).CtrlCaption
' Return reference to form module.
Set mdl = frm.Module
' Add event procedure.
lngReturn = mdl.CreateEventProc(&quot;Click&quot;, ctl.NAME)
' Insert text into body of procedure.
mdl.InsertLines lngReturn + 1, vbTab & dlgctrls.Ctrl(intControl).CtrlAction
End If
End Sub
Private Sub RealignControls()
'This function aligns Buttons right and Down
'Textboxs and comboboxes left and down
For Each ctl In frm.Controls
' Check to see if control is CommandButton.
If ctl.ControlType = acCommandButton Then
' Set control properties.
With ctl
.Left = frm.Width + frm.GridX - (.Width * 2) 'ie right hand edge - twice width of button
.Enabled = True
.Top = ((RemoveText(ctl.NAME)) * (ctl.Height * 2)) 'Button Nos start at 1
End With
Else
' Check to see if control is Label.
If ctl.ControlType = acLabel Then
With ctl
.Left = frm.GridX + (.Width * 0.5) 'ie left hand edge - twice width
.Top = ((RemoveText(ctl.NAME)) * (ctl.Height * 2)) 'Text Nos start at 0
End With
Else
With ctl
.Left = frm.GridX + (.Width * 1.5) 'ie left hand edge - twice width
.Enabled = True
.Top = ((RemoveText(ctl.NAME) + 1) * (ctl.Height * 2)) 'Text Nos start at 0
End With
End If
End If
Next ctl
End Sub
Public Function RemoveText(strInput) As String
Dim intCount As Integer
RemoveText = &quot;&quot;
intCount = 1

While intCount < Len(strInput) + 1
If Mid(strInput, intCount, 1) >= &quot;0&quot; And _
Mid(strInput, intCount, 1) <= &quot;9&quot; Then
RemoveText = Mid(strInput, intCount, 1)
End If
intCount = intCount + 1
Wend

End Function


As this module stands you may have up to ten controls, and you need to specify what type of control, its caption, label and the OnClick action. All this can obviously be enhanced so that you could specify the event etc.

Once the controls have been defined they are placed on the form labels on the left, text/combo in the middle and buttons on the right, again all this could be enhanced so that you define where each control appears, its font, size etc.

To use (This is the messy bit!!):
Private Sub Test_DialogBox_Click()
Dim ctrls As dlgControls

ctrls.Ctrl(1).CtrlType = acTextBox
ctrls.Ctrl(1).CtrlLabel = &quot;Text Box Label&quot;
ctrls.Ctrl(2).CtrlType = acCommandButton
ctrls.Ctrl(2).CtrlCaption = &quot;Exit&quot;
'Build Code to execute
ctrls.Ctrl(2).CtrlAction = &quot;Dim frm As Form&quot; & vbCrLf & _
&quot;Dim ctl As Control&quot; & vbCrLf & vbCrLf & _
&quot;Set frm = Form&quot; & vbCrLf & _
&quot;Set ctl = Screen.ActiveControl&quot; & vbCrLf & vbCrLf & _
&quot;DialogBox.DlgRetVal.ReturnCtrl(RemoveText(ctl.NAME) + 1).ButtonPress = vbOK&quot; & vbCrLf & vbCrLf & _
&quot;For Each ctl In frm.Controls&quot; & vbCrLf & _
&quot;'Check to see if control is text box.&quot; & vbCrLf & _
&quot;If ctl.ControlType = acTextBox Or _&quot; & vbCrLf & _
&quot;ctl.ControlType = acComboBox Then&quot; & vbCrLf & _
&quot;DialogBox.DlgRetVal.ReturnCtrl(RemoveText(ctl.NAME) + 1).CtrlValue = ctl.Value&quot; & vbCrLf & _
&quot;End If&quot; & vbCrLf & _
&quot;Next ctl&quot; & vbCrLf & vbCrLf & _
&quot;DoCmd.Close acForm, me.NAME, acSaveyes&quot;
ctrls.Ctrl(3).CtrlType = acCommandButton
ctrls.Ctrl(3).CtrlCaption = &quot;Click&quot;
ctrls.Ctrl(3).CtrlAction = &quot;msgbox &quot;&quot;Way Cool!&quot;&quot;&quot;
dBox &quot;Dialog Demo&quot;, ctrls
MsgBox DialogBox.DlgRetVal.ReturnCtrl(1).CtrlValue & &quot; &quot; & DialogBox.DlgRetVal.ReturnCtrl(2).ButtonPress
End Sub


Obviously this is fairly quick and dirty and I suspect a bit more error checking could be installed but hey ho!!

Enjoy!!

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top