Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9

    Focus combobox

    Maybe one of you can explain to me why the focus does not move to cboPTrayEnvelop using following code (msgboxes report both cboPTrayA4):



    Code:
    For intPass = 1 To 2 Step 1
    If intPass = 1 Then
    With Me.cboPTrayA4
    .SetFocus
    MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
    For lListIndex = .ListCount - 1 To 0 Step -1
    .RemoveItem (lListIndex)
    Next lListIndex
    .SelText = ""
    End With
    ' populate paperbin cboPTrayA4
    modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
    Else
    With Forms!frmAfdrukkenOp!cboPTrayEnvelop
    .SetFocus
    MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
    For lListIndex = .ListCount - 1 To 0 Step -1
    .RemoveItem (lListIndex)
    Next lListIndex
    .SelText = ""
    End With
    ' populate paperbin cboPTrayEnvelop
    modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
    End If
    Next intPass

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Does .RemoveItem and .SelText work for cboPTrayEnvelop? Maybe try adding .dropdown just below .setfocus and see if it errors on .dropdown.

  3. #3
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9
    After selecting from a combobox an other installed printer for this application it empties both paperbin comboboxes, filles them with the bins of the selected printer and shows in both an empty field. The user can then select a bin for paper and a bin for envelop. This is completely done for PTrayA4, but partly for PTrayEnvelop. It empties the combobox, filles it with the bins of the selected printer, but it doesn't show empty field. For that reason I added the msgboxes to tell me where the focus is. Both messages report cboPTrayA4.
    I have also done what you suggested ItsMe and added .dropdown just below the second .setfocus. This did not result in an error. After that I added the same after the first .setfocus, but n o error either.

  4. #4
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9
    I have also added a screendump of the situation. Might be helpfull with my explanation of the problem.
    Attached Thumbnails Attached Thumbnails Knipsel.PNG  

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Intruder1500 View Post
    I have also done what you suggested ItsMe and added .dropdown just below the second .setfocus. This did not result in an error. After that I added the same after the first .setfocus, but n o error either.
    It seems the code in your Else statement is not firing. There are only two possibilities if the code does fire. The code has an exception when the .dropdown line tries to fire because the focus is not on the combo OR you get focus and the combo drops down in a big and obvious way. Maybe there could be something funny going on if there is not anything to populate the rowsource of said combo.

    The message boxes you have do not seem to have anything that distinguishes one criteria from the other within the If Then Else statement. I would not rely on them to troubleshoot. I see the Activecontrol thing but, this may not be enough to distinguish which line is firing. It would appear only the first msgbox is getting to fire and not the second msgbox. Change Gotcha to something unique in the second and then determine why the second set of criteria is not met.

  6. #6
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9
    I changed 'Gotcha' and saw this messagebox coming up. So Else part was fired. In addition to the screendump in the third combobox (cboPTrayEnvelop) still the selected bin of the previous selected printer is in view, but in the selections the bins of the current printer are available.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I created a new unbound form and placed a command button and two combos. I named the combos and pasted the following code in the click event of my button. It worked fine. The first combo got focus and Gave me the correct name in msgbox then, the second msgbox with the correct name and set focus.

    Code:
    For intPass = 1 To 2 Step 1
    If intPass = 1 Then
    With Me.cboPTrayA4
    .SetFocus
    MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
    For lListIndex = .ListCount - 1 To 0 Step -1
    '.RemoveItem (lListIndex)
    Next lListIndex
    .SelText = ""
    End With
    '' populate paperbin cboPTrayA4
    'modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
    Else
    'With Forms!frmAfdrukkenOp!cboPTrayEnvelop
    With cboPTrayEnvelop
    .SetFocus
    MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
    For lListIndex = .ListCount - 1 To 0 Step -1
    '.RemoveItem (lListIndex)
    Next lListIndex
    .SelText = ""
    End With
    '' populate paperbin cboPTrayEnvelop
    'modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
    End If
    Next intPass
    That would leave some sort of issue with Forms!frmAfdrukkenOp!cboPTrayEnvelop

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I agree with ItsMe... I would use
    Code:
     With Me.cboPTrayEnvelop
    instead of
    Code:
     With Forms!frmAfdrukkenOp!cboPTrayEnvelop
    Are "cboPTrayA4" and "cboPTrayEnvelop" unbound combo boxes?
    Are the row sources set to "Value List"?


    Also:
    Code:
        For intPass = 1 To 2 Step 1
            If intPass = 1 Then
                With Me.cboPTrayA4
                    .SetFocus
                    MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
                    For lListIndex = .ListCount - 1 To 0 Step -1
                        .RemoveItem (lListIndex)
                    Next lListIndex
                    .SelText = ""
                End With
                ' populate paperbin cboPTrayA4
                modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
            Else
    '            With Forms!frmAfdrukkenOp!cboPTrayEnvelop
                With Me.cboPTrayEnvelop
                    .SetFocus
                    MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
                    For lListIndex = .ListCount - 1 To 0 Step -1
                        .RemoveItem (lListIndex)
                    Next lListIndex
                    .SelText = ""
                End With
                ' populate paperbin cboPTrayEnvelop
                modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
            End If
        Next intPass
    In both parts of the IF() statement, you are filling the same combo box (in BLUE). Is that correct?
    (I'm not sure what "modPrinter.GetBinList" does)



    I realize that the full code was not posted, but looking at your code, it could be written as
    Code:
        With Me.cboPTrayA4
            .SetFocus
            MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
            For lListIndex = .ListCount - 1 To 0 Step -1
                .RemoveItem (lListIndex)
            Next lListIndex
            .SelText = ""
        End With
        ' populate paperbin cboPTrayA4
        modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
        
        
        '    With Forms!frmAfdrukkenOp!cboPTrayEnvelop
        With Me.cboPTrayEnvelop
            .SetFocus
            MsgBox Screen.ActiveControl.Name, 0, "Gotcha!"
            For lListIndex = .ListCount - 1 To 0 Step -1
                .RemoveItem (lListIndex)
            Next lListIndex
            .SelText = ""
        End With
        ' populate paperbin cboPTrayEnvelop
        modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
    The For..Next loop and the IF() statement are not really needed...

    My $0.02....

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by ssanfu View Post
    ...I realize that the full code was not posted, but looking at your code, it could be written as....
    Yah, I am not sure what the missing code does either. Must be a UDF that gets a list of connected devices (printers). I am sure the original code could use some refactoring. It did seem to work though. Maybe this thread is solved.

  10. #10
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9
    The code GetBinList, which was found on internet, is now included. In my first attempt I did not use Form!frmAfdrukkenOp.cboPTrayEnvelop, For...Next loop, Public intPass and filled both comboboxes in GetBinList. These changes were included to get the focus on the cboPTrayEnvelop an to add "". I will go back to the first attempt, include your advices and come back with my findings.

    Code:
    Option Compare Database
    Option Explicit
    Public intPass As Integer
    ' Declaration for the DeviceCapabilities function API call.
    Private Declare Function DeviceCapabilities Lib "winspool.drv" _
        Alias "DeviceCapabilitiesA" (ByVal lpsDeviceName As String, _
        ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _
        ByVal lpDevMode As Long) As Long
        
    ' DeviceCapabilities function constants.
    Private Const DC_PAPERNAMES = 16
    Private Const DC_PAPERS = 2
    Private Const DC_BINNAMES = 12
    Private Const DC_BINS = 6
    Private Const DEFAULT_VALUES = 0Sub GetBinList(strPrinter As String)
    ' Uses the DeviceCapabilities API function to display a
    ' message box with the name of the default printer and a
    ' list of the paper bins it supports.
    Dim lngBinCount As Long
    Dim lngCounter As Long
    Dim hPrinter As Long
    Dim strDeviceName As String
    Dim strDevicePort As String
    Dim strBinNamesList As String
    Dim strBinName As String
    Dim intLength As Integer
    Dim strMsg As String
    Dim aintNumBin() As Integer
    On Error GoTo GetBinList_Err
    ' Get name and port of the default printer.
    strDeviceName = Application.Printers(strPrinter).DeviceName
    strDevicePort = Application.Printers(strPrinter).Port
    ' Get count of paper bin names supported by printer.
    lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_BINNAMES, _
    lpOutput:=ByVal vbNullString, _
    lpDevMode:=DEFAULT_VALUES)
    ' Re-dimension array to count of paper bins.
    ReDim aintNumBin(1 To lngBinCount)
    ' Pad variable to accept 24 bytes for each bin name.
    strBinNamesList = String(Number:=24 * lngBinCount, Character:=0)
    ' Get string buffer of paper bin names supported by printer.
    lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_BINNAMES, _
    lpOutput:=ByVal strBinNamesList, _
    lpDevMode:=DEFAULT_VALUES)
    ' Get array of paper bin numbers supported by printer
    lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_BINS, _
    lpOutput:=aintNumBin(1), _
    lpDevMode:=0)
    ' List available paper bin names.
    If lngBinCount > 0 Then
    For lngCounter = 1 To lngBinCount
    ' Parse a paper bin name from string buffer.
    strBinName = Mid(String:=strBinNamesList, _
    Start:=24 * (lngCounter - 1) + 1, _
    Length:=24)
    intLength = VBA.InStr(Start:=1, _
    String1:=strBinName, String2:=Chr(0)) - 1
    strBinName = Left(String:=strBinName, _
    Length:=intLength)
    ' populate the Paper Bin Combo box with a list of available bins
    If intPass = 1 Then
    Forms!frmAfdrukkenOp.cboPTrayA4.AddItem Item:=strBinName
    End If
    If intPass = 2 Then
    Forms!frmAfdrukkenOp.cboPTrayEnvelop.AddItem Item:=strBinName
    End If
    Next lngCounter
    End If
    GetBinList_End:
    Exit Sub
    GetBinList_Err:
    MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
    title:="Error Number " & Err.Number & " Occurred"
    Resume GetBinList_End
    End Sub
    Sub GetPaperList()
    ' Uses DeviceCapabilities API function to display a message box
    ' with the name of the default printer and a list of
    ' the papers it supports.
    Dim lngPaperCount As Long
    Dim lngCounter As Long
    Dim hPrinter As Long
    Dim strDeviceName As String
    Dim strDevicePort As String
    Dim strPaperNamesList As String
    Dim strPaperName As String
    Dim intLength As Integer
    Dim strMsg As String
    Dim aintNumPaper() As Integer
    On Error GoTo GetPaperList_Err
    ' Get the name and port of the default printer.
    strDeviceName = Application.Printer.DeviceName
    strDevicePort = Application.Printer.Port
    ' Get the count of paper names supported by printer.
    lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_PAPERNAMES, _
    lpOutput:=ByVal vbNullString, _
    lpDevMode:=DEFAULT_VALUES)
    ' Re-dimension the array to the count of paper names.
    ReDim aintNumPaper(1 To lngPaperCount)
    ' Pad the variable to accept 64 bytes for each paper name.
    strPaperNamesList = String(64 * lngPaperCount, 0)
    ' Get the string buffer of all paper names supported by the printer.
    lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_PAPERNAMES, _
    lpOutput:=ByVal strPaperNamesList, _
    lpDevMode:=DEFAULT_VALUES)
    ' Get the array of all paper numbers supported by the printer.
    lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_PAPERS, _
    lpOutput:=aintNumPaper(1), _
    lpDevMode:=DEFAULT_VALUES)
    ' List the available paper names.
    For lngCounter = 1 To lngPaperCount
    ' Parse a paper name from the string buffer.
    strPaperName = Mid(String:=strPaperNamesList, _
    Start:=64 * (lngCounter - 1) + 1, Length:=64)
    intLength = VBA.InStr(Start:=1, String1:=strPaperName, String2:=Chr(0)) - 1
    strPaperName = Left(String:=strPaperName, Length:=intLength)
    ' populate the Paper Size Combo box with a list of available paper sizes
    ' Forms!frmPrinter.cboPaperSize.AddItem Item:=strPaperName
    Next lngCounter
    GetPaperList_End:
    Exit Sub
    GetPaperList_Err:
    MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
    title:="Error Number " & Err.Number & " Occurred"
    Resume GetPaperList_End
    End Sub

  11. #11
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    OK, that helped. I understand why you have "intPass" in the code. Forget what I said and the changes I previously posted.

    I might have written the code using a different method, but that is what is nice about programming - everyone has a different style.

    Maybe one of you can explain to me why the focus does not move to cboPTrayEnvelop using following code (msgboxes report both cboPTrayA4):
    Back to your question: Does it matter if the focus is set to "cboPTrayEnvelop" or not?

  12. #12
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9
    I just modified the code to what it was and included your advices. The finding is now that .seltxt="" results in error 2115. The current code on open and on click cboInstalledPrinters is below. The previous given GetBinList fills now both papertray boxes in one go.

    on open:
    - find default system printer
    - fill cbo with installed printers
    - fill cboA4 and envelop with bins of system printer
    This works as expected. The three comboboxes show empty fields, but each selection list is filled. The first with the installed printers and the next two with the trays of the default printer. This also works as expected when I change the default printer in Windows.

    on click cboInstalledPrinter after selecting an other installed printer
    - first empty cboA4 and cboenvelop
    - and than fill cboA4 and cboenvelop with bins of selected printer
    In this on click event the error occurs when .seltxt="" is added to cbo. The first time the printer has changed in cboPTrayA4 and every next time the printer is changed in cboPTrayEnvelop. When statement 'On Error Resume Next' is added these error do not stop the code and from then the focus does not move anymore from the one combobox to the other. By the way both comboboxes are unbound and type of rowsources are set to value list.

    Code:
    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo Err_Form_Open
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim lListIndex As Long
    Dim prt As Access.Printer
    ' display default system printer
    Application.Printer = Nothing
    txtDefaultPrinter.Value = Application.Printer.DeviceName
    ' populate cboInstalledPrinters
    For Each prt In Application.Printers
    Me!cboInstalledPrinters.AddItem Item:=prt.DeviceName
    Next prt
    ' populate cboPTrayA4 and cboPTrayEnvelop based on default system printer
    modPrinter.GetBinList (Me!txtDefaultPrinter.Value)
    Set db = CurrentDb()
    ' if table EigenaarDruktAfOp exists retrieve stored Aalles printer and trays
    If TableExists("EigenaarDruktAfOp") Then
    Set rs = db.OpenRecordset("EigenaarDruktAfOp")
    cboInstalledPrinters = Nz(rs("AallesPrinter").Value, "")
    cboPTrayA4 = Nz(rs("AallesPrinterladeA4").Value, "")
    cboPTrayEnvelop = Nz(rs("AallesPrinterladeEnvelop").Value, "")
    optAfdrukkenOp = Nz(rs("AfdrukkenOp").Value, "")
    End If
    ' set focus to cboInstalledPrinters
    Me.cboInstalledPrinters.SetFocus
    Exit_Form_Open:
    Exit Sub
    Err_Form_Open:
    MsgBox Err.Description
    Resume Exit_Form_Open
    End Sub
    
    Private Sub cboInstalledPrinters_Click()
    Dim prt As Access.Printer
    Dim lListIndex As Long
    ' get printer object for selected Aalles printer
    Set prt = Application.Printers(Me!cboInstalledPrinters.Value)
    ' set selected Aalles printer as default printer for this session
    Set Application.Printer = prt
    ' empty cboPTrayA4 and cboPTrayEnvelop
    On Error Resume Next
    With Me.cboPTrayA4
    .SetFocus
    MsgBox Screen.ActiveControl.Name, 0, "cboPTrayA4!"
    For lListIndex = .ListCount - 1 To 0 Step -1
    .RemoveItem (lListIndex)
    .SelText = "" ' the first time changing the printer this statement results in error 2115!
    Next lListIndex
    End With
    With Me.cboPTrayEnvelop
    .SetFocus
    MsgBox Screen.ActiveControl.Name, 0, "cboPTrayEnvelop!"
    For lListIndex = .ListCount - 1 To 0 Step -1
    .RemoveItem (lListIndex)
    Next lListIndex
    .SelText = "" ' every next time the printer is changed this statement results in error 2115!
    End With
    ' populate paperbin cboPTrayA4 and cboPTrayEnvelop based on cboInstalledPrinters
    modPrinter.GetBinList (Me!cboInstalledPrinters.Value)
    End Sub

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I am not sure I understand selecting an empty string. Perhaps don't include that line. Does not seem to be doing anything even if Access allowed it to happen.

  14. #14
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    .SelText = ""
    From Help: The SelText property is read-only.
    This means you cannot change the value of a control using SelText. (which results in error 2115)

    Try
    .Value = ""
    (or .Value = Empty)
    (or .Value = NULL)

    ---------------------------------------------

    You try replacing:
    Code:
          For lListIndex = .ListCount - 1 To 0 Step -1
             .RemoveItem (lListIndex)
          Next lListIndex
    with
    Code:
      .RowSource = ""
    It would look like
    Code:
       With Me.cboPTrayEnvelop
          .SetFocus
          MsgBox Screen.ActiveControl.Name, 0, "cboPTrayEnvelop!"
          .RowSource = ""
          .Value = ""
       End With
    --------------------------------------------
    In the sub "form_Open", I don't think you should have the line
    " Application.Printer = Nothing"
    Setting an object to NOTHING destroys the pointer to it.

    Because you "Set db = " and "Set rs = ",
    when you exit the Sub (Function), you should close any record sets created, then remove the pointers.

    Code:
    Exit_Form_Open:
       ' ***I always do this***
       rs.Close
       Set rs = Nothing
       Set db = Nothing
       Exit Sub
    The rule is: If you open it, close it. If you create it, destroy it.
    That means you open a record set, you close it, but you do not close "db" because you did not open it. So you just destroy the pointer.

    ------------------------------------------------
    BTW, in "cboInstalledPrinters_Click()", there is a line that should be moved out of the For..Next loop:
    Code:
       With Me.cboPTrayA4
          .SetFocus
          MsgBox Screen.ActiveControl.Name, 0, "cboPTrayA4!"
          For lListIndex = .ListCount - 1 To 0 Step -1
             .RemoveItem (lListIndex)
             .SelText = ""   ' the first time changing the printer this statement results in error 2115!
          Next lListIndex
       End With

    -------------------------------

    OK, enough of my ramblings. Your new code looks cleaner. Just a couple of minor changes recommended.

  15. #15
    Intruder1500 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    The Netherlands
    Posts
    9
    Quote Originally Posted by ItsMe View Post
    I am not sure I understand selecting an empty string. Perhaps don't include that line. Does not seem to be doing anything even if Access allowed it to happen.
    It should clear the selection in the combobox. It does the job for cboPTrayA4. See the screenshot in one of my previous messages. The problem is it doesn't do that for cboPTrayEnvelop, but comes with an error.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-30-2013, 07:18 AM
  2. Replies: 1
    Last Post: 09-06-2011, 01:47 PM
  3. Replies: 0
    Last Post: 02-22-2011, 04:18 PM
  4. Replies: 0
    Last Post: 08-24-2010, 06:38 PM
  5. Replies: 0
    Last Post: 12-16-2009, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums