Results 1 to 11 of 11

Thread: [RESOLVED] Access VBA Events

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Location
    Massachusetts
    Posts
    65

    Resolved [RESOLVED] Access VBA Events

    Hi All,

    I started a project that seemed simple enough at first, but has since spiraled out of control. I'm looking to automate one of our processes by using the value of one field to determine another.

    Firstly, I'm using Access 2007. What I want to do is figure out a field called "placement" from a student's test scores. That part was difficult but I figured it out. The next thing I want to do, I'm getting stuck on. I want to now calculate an assessment value field based on placement. For example, if placement is 100, assessment value should be 50. My problem is, the only events that will allow the assessment value field to populate are "on click" and "lost focus".

    I don't want to have to go through all the records and click on placement in order to populate assessment value. That would defeat the purpose, since I plan to import the test scores that placement is based on in the first place. I want the form to populate the table and not vice versa. Is there any way to do this? Thanks in advance!

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Access VBA Events

    1)
    if you manually populate your form from unbound source, you can code in the update

    2)
    on active event?

    3)
    put it in a field too. After import, run an update query that sets it

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Access VBA Events

    dataempress

    I use Access alot, but typically "drive it" using VB6. So, for example,
    I might have a form with a button on it, and if I click that button,
    it will loop through all the records in a recordset and do something.

    You seem to be wanting to do this using Access VBA.
    --1. Do you have a form that displays the data in field "placement"?
    --2. Could you add a button to that form?


    If yes to 2, that could be the way to capture the "event". You
    could then put your code within that button click sub.

    Does that help?
    Spoo

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Access VBA Events

    Quote Originally Posted by dataempress View Post
    Hi All,

    I started a project that seemed simple enough at first, but has since spiraled out of control. I'm looking to automate one of our processes by using the value of one field to determine another.

    Firstly, I'm using Access 2007. What I want to do is figure out a field called "placement" from a student's test scores. That part was difficult but I figured it out. The next thing I want to do, I'm getting stuck on. I want to now calculate an assessment value field based on placement. For example, if placement is 100, assessment value should be 50. My problem is, the only events that will allow the assessment value field to populate are "on click" and "lost focus".

    I don't want to have to go through all the records and click on placement in order to populate assessment value. That would defeat the purpose, since I plan to import the test scores that placement is based on in the first place. I want the form to populate the table and not vice versa. Is there any way to do this? Thanks in advance!
    If your field is a calculated field you are better off having it generated on the fly (unless it seriously impedes performance - otherwise you risk data becoming out of date in your calculated field if changes aren't propagated) you can either point the record source of a form at the query or modify the control source of the field which is calculated to read something like the following:

    =[field1]*[field2]

    I would prefer the second option; as queries are really for retrieval of data rather than update For your information, if you want to trigger an event every time the record pointer is changed, use the form OnCurrent event.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Location
    Massachusetts
    Posts
    65

    Re: Access VBA Events

    Hi All,

    Thank you so much for responding. I was away from my desk for a few days so I'm just catching up with the replies.

    To answer Spoo's questions:

    1. Do you have a form that displays the data in field "placement"? Yes
    2. Could you add a button to that form? Yes

    The only reason I didn't do this was because I was coding If Then events in the on click events of the button, and I had to go through it record by record, which seemed to defeat the purpose for me. I never even thought of looping. Honestly, I have the hardest time wrapping my brain around looping in VB. I can do it okay in Java but in VB I'm never sure which loop to use or how the formats are set up. So I end up writing far too many If/Then statments. If you could give me any insight on how to loop through a recordset I'd really appreciate it.

    I have to do an update query at the outset, in order to get my information imported into the table. The table is filled with many fields and I only need to import about 5 fields into specific records. I really wish there was a way to just import the fields you need into the records you need. So I have to import the information into a temporary table and run an update query.

    I wish I could make my destination field calculated. That would solve the problem. Unfortunately, I didn't make the database, so I don't really have the power to change a field. That's why I was calculating a value in my calculated field and using the afterupdate event to try to populate my destination field.

    Am I making any sense?

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Access VBA Events

    dataempress

    The following code frag gives the jist of looping
    through a recordset (using DAO -- ADO would be
    similar). It is written in VB6, but VBA should be similar

    Code:
    Dim DB99 As Database
    Dim RS99 As Recordset
    Set DB99 = Workspaces(0).OpenDatabase("c:\my stuff\myMDB.mdb")
    Set RS99 = DB99.OpenRecordset("myRS", dbOpenTable)
    rc = RS99.RecordCount
    RS99.MoveFirst
    For ii = 1 to rc
        ' 1. extract value of field placement
        v1 = RS99("placement")
        ' 2. calculate the value for assessment (your algorithm may be different)
        v2 = v1 / 2
        ' 3. put the value into the RS
        RS99.Edit
        RS99("assessment") = v2
        RS99.Update
        RS99.MoveNext
    Next ii
    Hope that helps.
    Holler if you need any clarifications.

    Spoo

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Location
    Massachusetts
    Posts
    65

    Re: Access VBA Events

    With Access 2007, I'm not sure whether to use ADO or DAO. Do I have to set any references?

    Set DB99 = Workspaces(0).OpenDatabase("c:\my stuff\myMDB.mdb")
    With this line of code, do I have to open the database? This might seem like a stupid question, but if I'm in it, isn't it already open?

    Set RS99 = DB99.OpenRecordset("myRS", dbOpenTable)
    In this line of code, would I be able to specify certain records, since not every record in my database needs to be changed?

    Set RS99 = DB99.OpenRecordset("myRS", dbOpenTable)
    Does this recordset need to be from a table, or can it be from a query? Can "myRS" be the name of a query?

    ii = 1 to rc
    ' 1. extract value of field placement
    v1 = RS99("placement")
    ' 2. calculate the value for assessment (your algorithm may be different)
    v2 = v1 / 2
    ' 3. put the value into the RS
    If placement=x; assessment=y, would I put that statement under v2?

    Thank you so much for your reply. I've just never looped successfully in VB, and I want to get this right. I know it's important to learn.

  8. #8
    Member
    Join Date
    Jul 2009
    Location
    Oregon
    Posts
    60

    Re: Access VBA Events

    I use ADO for my Access 2007 programs. The reference will depend on the version of Access that is being used. For example, I have Access 2007 so I naturally picked the latest version of ADO which is Microsoft ActiveX Data Objects 2.8 Library, but since the program I was developing was for a group that still uses Access 2000 I had to change the reference to 2.1 and had to alter some of the code.

    This is how I would declare the objects of an ADO Connection(your database) and a Recordset(Table/Query)
    vb Code:
    1. Public myDB As ADODB.Connection
    2. Public myRS As ADODB.Recordset

    There are a couple of ways to set the Connection object(which I will refer to as myDB from now on)...
    1) If you are accessing a table from the same database, you can use the following code...
    vb Code:
    1. Set myDB = CurrentProject.AccessConnection

    2) If the recordset you are looking to connect to is in another database, you will need the following...
    vb Code:
    1. Set myDB = New ADODB.Connection
    ----With this setup there is an additional step that you will need to perform, and I do it with the following code
    vb Code:
    1. With myDB
    2.     .Provider = "Microsoft.Access.OLEDB.10.0"
    3.     .Properties("Data Provider").Value = "Microsoft.Jet.OLEDB.4.0"
    4.     .Properties("Data Source").Value = ***path/name of db here***
    5.     .Open
    6. End With

    Now for the Recordset(which I will refer to as myRS from now on)...
    vb Code:
    1. Set myRS = New ADODB.Recordset
    ---This is how you will instantiate the myRS(or whatever your Recordset object is called)

    To give myRS some values you will need to do the following...
    vb Code:
    1. With myRS
    2.     Set .ActiveConnection = myDB     <-----setting the recordsets home to the myDB
    3.     .Source = "SELECT * FROM yourTable"  <----SQL Statement string that will grab the data that you specify
    4.     .LockType = adLockOptimistic <----I usually set my Recordset LockType property as this because I frequently am updating the data within my recordsets
    5.     .Open
    6. End With

  9. #9
    Member
    Join Date
    Jul 2009
    Location
    Oregon
    Posts
    60

    Re: Access VBA Events

    for the looping I would use a For...Next iteration to go through the records...

    This is how I would implement that...

    1) you will need to declare an Interger data type variable and a variable to hold the RecordCount as an Integer data type
    vb Code:
    1. Dim i as Integer
    2. Dim myRecCount as Integer

    2) assign value to the variables...
    vb Code:
    1. i = 1  'Sets to record 1
    2. myRecCount = myRS.RecordCount 'This will get the number of records in the Recordset you have selected

    3) create the iteration...
    vb Code:
    1. For i to myRecCount
    2.  
    3.                              '****Your Code Here*****
    4.  
    5. Next

  10. #10
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Access VBA Events

    dataempress

    With Access 2007, I'm not sure whether to use ADO or DAO. Do I have to set any references?
    I'm not sure. I referenced DAO as that is the only format that I am familiar with.


    Set DB99 = Workspaces(0).OpenDatabase("c:\my stuff\myMDB.mdb")
    With this line of code, do I have to open the database? This might seem like a stupid question, but if I'm in it, isn't it already open?
    That line of code "opens" the database.


    Set RS99 = DB99.OpenRecordset("myRS", dbOpenTable)
    In this line of code, would I be able to specify certain records, since not every record in my database needs to be changed?
    I'm not certain how to answer this. My inclination is that the above statement
    would be used to "open" the record set. A subsequent statement may be
    required to specify the query.


    Set RS99 = DB99.OpenRecordset("myRS", dbOpenTable)
    Does this recordset need to be from a table, or can it be from a query? Can "myRS" be the name of a query?
    Possibly. I'm afraid I don't work enough with queryies to be able to answer.


    Code:
    ii = 1 to rc
       ' 1. extract value of field placement
       v1 = RS99("placement")
       ' 2. calculate the value for assessment (your algorithm may be different)
       v2 = v1 / 2
       ' 3. put the value into the RS
        RS99.Edit
        RS99("assessment") = v2
        RS99.Update
        RS99.MoveNext
    If placement=x; assessment=y, would I put that statement under v2?
    I was being a little pedantic in my example, wherein the separate
    variables v1 and v2 were used to emphasize the steps involved.

    To your direct questions:

    -- If placement=x, then v1=x. v1 = RS99("placement") assigns the value
    of field "placement" to the variable v1 for the given record being "looked at"

    -- If assessment=y, the v2=y. My understanding is that you want to
    calculate a value for "assessment". I have made the simplifying assumption
    that you merely divided by 2. If I am wrong, then this would be the line
    at which you could express the proper algorithm.

    Actually populating the field "assessment" takes several lines of code.
    Those are presented in step 3, put the value into the RS.

    Does that help?

    Spoo

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Location
    Massachusetts
    Posts
    65

    Re: Access VBA Events

    Hi All,

    Thank you so much for your help. I really appreciate you taking the time to give such detailed replies. I have a better understanding of looping and that seems like a good solution. I will give that a try. Thanks again!

Tags for this Thread

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