Results 1 to 10 of 10

Thread: [RESOLVED] Excel and Access

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Resolved [RESOLVED] Excel and Access

    Hello, I have a table in access that has Name, Age, Address, Phone columns. I also have an excel worksheet that has headers for Name, Age, Address, Phone. I have never really done office development before, but how would I go about doing the following?

    If I type the Name of someone under the name header, it will automatically pull the rest of the data associated with that name (Age, Address, Phone) from the access database...and put it in the 3 columns to the right of the name in the same row?
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Excel and Access

    If you use VBA in excel, you could open an ADO connection with the query
    "Select * from tablename where tablename = " & Cells(A,1)

    or however you reference a cell in excel.

    Then just grab the appropriate column from the resulting dataset

    HTH

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel and Access

    A problem you may encouter when running queries against this table is that Name is a reserved word in both VBA and VB.

    If this project is in the beginning states, I would change that to something different, otherwise, in any query in which this field is mentioned, you would need to encapsulate it in brackets, i.e.: [name]

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Excel and Access

    Alright, I have a simple example done...but what is the command to get the current cell your in?
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Excel and Access

    I believe

    ActiveCell.Value

    would reference the current cell

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Excel and Access

    ActiveCel.Value gives me the value of whats in the current cell (ie: "Age"). What I am looking for, is the cells location.

    What I am looking for is, if I am in cell A2, and I type "Brandon" and hit enter...I want to know what row and col that I typed it in.
    Last edited by LostAngel; May 19th, 2006 at 10:09 AM.
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and Access

    Remove the .value and type the . again; you will then see a list of properties/methods relating to ActiveCell - one of which I believe is Row. I'm sure you can work out how to find the column too.

  8. #8
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Excel and Access

    I think

    ActiveCell.Row
    and
    ActiveCell.Column

    Would reference what you're looking for

    I've never used VBA in excel though


    *edit* wow, had the reply window open for 4 minutes before submitting aparently

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Excel and Access

    Thank you very much si_the_geek...I feel very dumb as I see the properties are very much like flexgrids and such...
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: Excel and Access

    This is my finished code:

    VB Code:
    1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    2. Dim oRs As ADODB.Recordset
    3.     Dim oCnn As ADODB.Connection
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    6.     Dim i As Integer
    7.     'Connect to your Access db
    8.     Set oCnn = New ADODB.Connection
    9.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    10.     "C:\Documents and Settings\brandone\Desktop\db1.mdb;Persist Security Info=False"
    11.     oCnn.Open
    12.  
    13.     'Create your recordset
    14.     Set oRs = New ADODB.Recordset
    15.     oRs.Open "SELECT Name, Address, Phone, Age FROM tblInfo WHERE Name = '" & Me.Cells(ActiveCell.Row - 1, 1) & "'; ", oCnn, adOpenKeyset, adCmdText
    16. 'MsgBox ActiveCell.Row
    17.     'oApp.Visible = True
    18.    
    19.     Sheet1.Cells(ActiveCell.Row - 1, 1).CopyFromRecordset oRs
    20.     oRs.Close
    21.     Set oRs = Nothing
    22.     oCnn.Close
    23.     Set oCnn = Nothing
    24. End Sub
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

Posting Permissions

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



Click Here to Expand Forum to Full Width