+ Reply to Thread
Results 1 to 6 of 6

Displaying Acces rows from a UserForm

Hybrid View

  1. #1
    Registered User
    Join Date
    11-08-2014
    Location
    london
    MS-Off Ver
    2013
    Posts
    49

    Displaying Acces rows from a UserForm

    Hi I was hoping for some help to be able to show a series of rows for a specific product reference (usually 3 or 4 rows) from my access DB in a userform.

    My access DB is called ActinicCatalog.MDB. I have the query in Access that brings back all the rows for all products.
    SELECT ProductProperties.nType, ProductProperties.nPropertyID, ProductProperties.nValue1, ProductProperties.bFlag1, ProductProperties.sProductRef, Product.[Product Reference], Product.[Short description], ProductProperties.nSequence
    FROM Product INNER JOIN ProductProperties ON Product.[Product Reference] = ProductProperties.sProductRef
    WHERE (((ProductProperties.nType)=8))
    ORDER BY Product.[Product Reference], ProductProperties.nSequence;
    I have 2 big stumbling blocks:

    The first is how to incorporate this into my userform (calling the query and displaying the results in a table).

    The second is I want to pass to the query a specific product reference ( Product.[Product Reference] ) so I just get the rows for that product

    Being honest I have not tried to get data from an Access DB into Excel before so a basic code structure would really help to get me on the right path.

    Many thanks

    Tony

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Displaying Acces rows from a UserForm

    The following code will run an SQL query on the database and return the results to the Excel spreadsheet, simply replace the details of your DB and path in the cnn.Open line and your own SQL query in the SQL= bit

    Private Sub CommandButton1_Click()
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sQRY As String
    Dim strFilePath As String
    Me.CommandButton1.Caption = "Please Wait"
    Me.Repaint
        Rows("2:5000").Select
        Selection.Delete Shift:=xlUp
        Range("A2").Select
    strFilePath = Range("dbase_path") & Range("dbase_name")
    Set cnn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strFilePath & ";"
    
    
    SQL = "SELECT * FROM `qryFullRequestDetails` WHERE (Order_Number > 10) ORDER BY tblRequests.Order_Number ASC"
    
    rs.CursorLocation = adUseClient
    rs.Open SQL, cnn, adOpenStatic, adLockReadOnly
    Application.ScreenUpdating = False
    Sheet1.Range("A2").CopyFromRecordset rs 'Where to place the data
    rs.Close
    Set rs = Nothing
    cnn.Close
    Set cnn = Nothing
    Unload Me
    End Sub
    I then have a userform which opens on this data when you double click a row. The form has the following code in it:

    Dim ws As Worksheet
    Dim lRow As Long
    Dim CurRecord As Long
    Public i As Integer
    Public origStatus As String
    Public caseId As Integer
    
    
    Private Sub UserForm_Initialize()
        Set ws = ThisWorkbook.Sheets("Sheet1")
            i = ActiveCell.Row
                refresh_form (i)
    End Sub
    
    Function refresh_form(i)
    If (Len(Range("A" & i).Value) < 1) Then
    i = i - 1
    Else
    '
    End If
                    caseId = Range("A" & i).Value
                    txtRequestName.Text = Range("AB" & i).Value
                    txtOrderNumber.Text = Range("C" & i).Value
                    txtReqPhone.Text = Range("AE" & i).Value
                    txtReqLocation.Text = Range("AC" & i).Value
                    txtReqBusUnit.Text = Range("AF" & i).Value
                    txtReqBusArea.Text = Range("AG" & i).Value
                    txtReqDelivery.Text = Range("E" & i).Value
                    txtReqDate.Text = Range("B" & i).Value
                    txtCustName.Text = Range("F" & i).Value
                    chkPreCred.Value = Range("K" & i).Value
                    txtInfo.Value = Range("M" & i).Value
                    txtAssignDate.Value = Range("O" & i).Value
                    txtAssignedTo.Value = Range("P" & i).Value
                    txtReturnDate.Value = Range("Q" & i).Value
                    ComboStatus.Value = Range("U" & i).Value
                    origStatus = Range("V" & i).Value
                    RecNo.Value = i - 1
    End Function
    
    Function selected_row(rowNum)
    Rows(i - 1).Font.Bold = False
    Rows(i + 1).Font.Bold = False
    Rows(i).Font.Bold = True
    Rows(i - 1).Font.ColorIndex = 1
    Rows(i + 1).Font.ColorIndex = 1
    Rows(i).Font.ColorIndex = 3
    Cells(i, ActiveCell.Column).Select
    refresh_form (i)
    End Function
    
    
    Private Sub PrevRecord_Click()
    i = i - 1
    If (i > 1) Then
        selected_row (i)
    End If
    End Sub
    
    Private Sub NextRecord_Click()
    i = i + 1
    If (i > 1) Then
        selected_row (i)
    End If
    End Sub
    Basically, all you do is assign the relevant cell value to each field, the PrevRecord and NextRecord buttons then allow you to move records, simply by adding or subtracting 1 from the row number and refreshing the form.
    If someone has helped you then please add to their Reputation

  3. #3
    Registered User
    Join Date
    11-08-2014
    Location
    london
    MS-Off Ver
    2013
    Posts
    49

    Re: Displaying Acces rows from a UserForm

    Thank you so much. I will have a go

  4. #4
    Registered User
    Join Date
    11-08-2014
    Location
    london
    MS-Off Ver
    2013
    Posts
    49

    Re: Displaying Acces rows from a UserForm

    Hi. Thank you once again.

    I have got the query returning results. They are the same as running it in Access apart from it is not excluding rows that have a '*!*. The code is WHERE ...... ((Product.[Product Reference]) Not Like'*!*' )

    
    Sql = "SELECT ProductProperties.nType, ProductProperties.nPropertyID, ProductProperties.nValue1, ProductProperties.bFlag1, ProductProperties.sProductRef, Product.[Product Reference], Product.[Short description], ProductProperties.nSequence, ProductProperties.sString1 FROM Product INNER JOIN ProductProperties ON Product.[Product Reference] = ProductProperties.sProductRef WHERE (((ProductProperties.nType)=8 Or (ProductProperties.nType)=7) AND  ((Product.[Product Reference]) Not Like '*!*')) ORDER BY Product.[Product Reference], ProductProperties.nSequence "
    Any ideas why it is ignoring this part?

    Many thanks

    Tony

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Displaying Acces rows from a UserForm

    Try replacing the wildcard * with %.
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    11-08-2014
    Location
    london
    MS-Off Ver
    2013
    Posts
    49

    Re: Displaying Acces rows from a UserForm

    Thank you. That's sorted

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help With Displaying Currency In A Userform
    By VBTroubles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2013, 09:36 AM
  2. UserForm not Displaying Correctly
    By Strugglin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-01-2009, 09:02 AM
  3. Loop to acces various rows
    By Mhek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2006, 05:10 PM
  4. Displaying one userform from another
    By CFD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-06-2005, 01:18 AM
  5. [SOLVED] PROBLEM DISPLAYING USERFORM
    By john.9.williams@bt.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2005, 09:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1