View Single Post
 
Old 09-14-2020, 06:45 AM
spinn2046@gmail.com spinn2046@gmail.com is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Aug 2020
Posts: 6
spinn2046@gmail.com is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Greg's use of functions is part of an effort to create modular code which over the long term enables you to reuse old code and save a lot of time. Many different projects need to get data from Excel. Greg's function enables him to copy and paste that chunk of code into many different projects and saves him lots of rework.
So this Greg's function:
Code:
Private Function fcnExcelDataToArray(strWorkbook As String, _
                                     Optional strRange As String = "Sheet1", _
                                     Optional bIsSheet As Boolean = True, _
                                     Optional bHeaderRow As Boolean = True) As Variant
in this situation, was designed to be called from this subroutine in the middle of the code "Sub Document_Open()", right?
Not by me or other user manually in the Excel cell, right??
It took me a while to grasp that...


Going back to my code...
What I want to do now with my code, preferably in the end of it, before this line:
Code:
    ' Display the results.
    ' txtBooks.Text = txt
is I need to find "strPESELfromWord" value in my array's (It's called "values") 1st row.
Can anybody help me with that?

This is my current code:

Code:
Sub GetRows_returns_a_variant_array()

Dim connection As ADODB.connection
Dim recSet As ADODB.Recordset
Dim exclApp As Excel.Application     'This code is written and lunched from Word VBA Editor
Dim exclWorkbk As Excel.Workbook
Dim mySheet As Excel.Worksheet

Dim wordDoc As Word.Document
Dim strPESELfromWord As String
Dim strQuery0 As String
Dim strQuery1 As String
Dim strQuery2 As String
Dim strSexDigit As String
Dim values As Variant                'Should there be values() ??
Dim txt As String
Dim intRemainder As Integer
Dim r As Integer
Dim c As Integer

    Set wordDoc = Word.ActiveDocument
    Debug.Print wordDoc.Name
    Set connection = New ADODB.connection
    Debug.Print "ADODB.connection.Version = " & connection.Version
    strPESELfromWord = Trim(Selection.Text)
    Debug.Print "Wybrano PESEL " & strPESELfromWord & "."

    strSexDigit = Mid(strPESELfromWord, 10, 1)      'Extract 10th digit from PESEL number
    Debug.Print strSexDigit
    intRemainder = strSexDigit Mod 2                'If 1 is remaider it means it's a male, and if there's no remainder, than it's a female.
    Debug.Print intRemainder

    ' Open the database connection.
    connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\custdb.xlsm;" & _
                "Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"                 'now it works

    ' Select the data.
    strQuery0 = "SELECT * FROM Books ORDER BY Title, Year"
    strQuery1 = "SELECT * FROM [data$]"      '[data$]   is the table name; in this case it's the sheet name;
    strQuery2 = "SELECT * FROM [data$] WHERE pesel <> ''"      'col B = pesel; col C = data_urzodzenia; col D = imie_nazwisko

    ' Get the records.
    Set recSet = connection.Execute(strQuery2, , adCmdText)

    ' Load the values into a variant array.
    values = recSet.GetRows            'GetRows returns a variant array holding the Recordset's values.

    ' Close the recordset and connection.
    recSet.Close
    connection.Close

    ' Use the array to build a string containing the results.
    For r = LBound(values, 2) To UBound(values, 2)
        For c = LBound(values, 1) To UBound(values, 1)        'max "c" = 78
           'txt = txt & values(c, r) & ", "
            txt = txt & values(c, r) & "| "
        Next c
        'txt = Left$(txt, Len(txt) - 1) & vbCrLf  'Left Returns a Variant (String) containing a specified number of characters from the left side of a string.
         txt = Left(txt, Len(txt) - 1) & vbCrLf   'The trailing $ is a type declaration character for the String data type in VBA.
    Next r                                       'The result returned from Left$() is a string, whereas Left() returns a Variant.
                              'You must use Left(), not Left$() if there is any chance of Null values, since the Variant can be Null but the String cannot.
                     
    ' Display the results.
    ' txtBooks.Text = txt

End Sub
Reply With Quote