Greetings,
I have a datasheet, created through VBA, and I am trying to automate setting the column widths to Best Fit. Currently I loop through the controls using the code below
which calls the function TBBestFit as seen below
This works okay (does anyone know a better way?), but occasionally the popup dialog asking what width the column should be is not active when it first appears. If I click on my selection, then the subsequent iterations flow just fine. So, my question is this: is there any way in VB to set the dialog box as active so the send keys function will work properly?
Thanks in advance for any help.
I have a datasheet, created through VBA, and I am trying to automate setting the column widths to Best Fit. Currently I loop through the controls using the code below
Code:
'Set width of columns to best fit data
Dim ctl As Control
For Each ctl In Forms![Form1].Controls
If (ctl.ControlType = acTextBox) Then
ctl.SetFocus
Call TBBestFit
End If
Next ctl
Code:
Public Function TBBestFit()
On Error GoTo ErrHandler
SendKeys "%B", False
DoCmd.RunCommand acCmdColumnWidth
Exit Function
ErrHandler:
Select Case Err
Case 2046
'Command not available
MsgBox "Column width is not available at this time.", vbCritical, "Not Available"
Case 2501
'Cancel selected in dialog box - do nothing
Case Else
MsgBox Err.Number & ":-" & vbCrLf & Err.Description
End Select
End Function
Thanks in advance for any help.
Comment