Match Excel Values with Access Query and in-put data

NickWren

New member
Local time
Yesterday, 22:52
Joined
Mar 28, 2017
Messages
8
I have an Excel file that I am trying to upload into MS Access, I can easily click on a subform (which allows me to call the SampleID) then find that SampleID on the Excel Spreadsheet in column A; which will in-put other columns in that same row.

However now I want to use a whole query's SampleID to check to see if any of them are in column A of a spreadsheet; if they are in-put data (easy vba codes for in-putting).

Would I use two loops? one to go through each sample of the query then through each sample of the excel files? Is there any other way to go about this? I havent gotten the two loops to work... which is why I am trying to see what would be the best way to do this.

Excel SpreadSheet
Code:
[B]Sample.ID          Data
[/B]Sample1             1.04142
Sample2             2.44058
Sample4             4.15872
Query
Code:
[B]Sample.ID          Data
[/B]Sample1                 0
Sample2                 0
Sample3                 0
Sample4                 0
Code:
Private Sub Command73_Click()
    Dim fd As FileDialog
    Dim NormalizedFileName As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .Filters.Add "Comma Delimited Files", "*.csv", 1
        .InitialFileName = "//Wrenlab02/research files/MULTIPLE MYELOMA/PCR DATA/MM Screen PCR/" & Year(Date) & "/"
        .InitialView = msoFileDialogViewDetails
        .AllowMultiSelect = False
        .Title = "Select CT Values to be imported for your Multiple-Myeloma Sample"
        If .Show Then
            selectFile = .SelectedItems(1)
             Dim xl              As Object
        Dim xlWrkBk         As Object
        Dim xlSht           As Object
        Set xl = CreateObject("Excel.Application")
        xl.Visible = False 'Control whether or not Excel should be visible to
                                'the user or not.
        Set xlWrkBk = xl.Workbooks.Open(selectFile)
        Set xlSht = xlWrkBk.Worksheets(1)
        
        Dim i As Integer
        Dim n As Integer
        n = 1
        i = 2
        LastRow = xlSht.UsedRange.Rows.Count
        Do While (i < LastRow)
        SampleName = xlSht.Cells(i, 1)
        Do While (n < [subformtblFacilityPatients].Form![Total])

            If SampleName = [subformtblFacilityPatients].Form![WrenID] Then
            Me.Text74 = [subformtblFacilityPatients].Form![WrenID]
            End If
            
            n = n + 1
            Loop
                        
            i = i + 1
                    Loop
        xlWrkBk.Close False 'Close the Workbook without saving any changes
        xl.Quit 'Close Excel
        Else
            End
        End If
    End With
    Set xlSht = Nothing
    Set xlWrkBk = Nothing
    Set xl = Nothing
    Set fd = Nothing
End Sub

This code might be hard to follow (copied from different websites and tried to combine it to make it work (not to much of a VBA coder myself but trying to understand it more as I keep trying)).

The code pretty much uploads a CSV/XLSX file; figures out the number of rows, and starts a loop (i) until it looks through all the number of rows.

Then the code will look at a textbox that has the total number of rows in the query; this will start a loop (n) until it reads all the query's sample names.

Lastly if the names match in both then print out the name (normally I would in-put data but I just wanted to see if it would even print out the matching name which it doesnt (Not even one which is what I was expecting)).
 
why not just link to the excel spreadsheet (or import it to a table) and then use a query linking 'whole query's SampleID' to the excel table on sampleID. No code or looping required
 
Because I think inside a box hahaha. Well thats an easy solution; I dont really want to use a linked table, but maybe I can upload the spreadsheet or maybe in-put the excel spreadsheet into a table to in-put it into the corresponding table.
 
you can import manually or use transferspreadsheet in vba or macro
 
CJ when you get a chance to look at this can you tell me how to link the sample ID field on my excel file and the one on the table? I used a relationship between the two but the data is not going into that sample for some reason when using an update query.

updatequery.jpg

The Sample ID and the Wren ID are the same; just different names from the file to the table.
 

Users who are viewing this thread

Back
Top Bottom