If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > how to run code in Current record ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-12, 03:22
oMADMANo oMADMANo is offline
Registered User
 
Join Date: Jan 2012
Posts: 16
how to run code in Current record ?

Okay i have a tick box called "Riskassessment" and on it being clicked I would like some items to be visible on that record. I have set a control source for this tick box to yes/no option in my Membership_Database and I am trying to make this code:
Code:
Private Sub RiskAssesment_Click()

 If rs!RiskAssesment = False Then

rs!Risk.Visible = False


 ElseIf rs!RiskAssesment = True Then

rs!Risk.Visible = True
only run on current record. also applied this to my form current sub.
Thanks.
Reply With Quote
  #2 (permalink)  
Old 01-29-12, 11:28
Missinglinq Missinglinq is offline
Registered User
 
Join Date: Jun 2005
Location: Richmond, Virginia USA
Posts: 1,702
Your use of rs in rs!RiskAssesment would suggest you're copping code from somewhere that uses a RecordSet, which really isn't appropriate here. This is the code you need to accomplish your goal:
Code:
Private Sub RiskAssesment_AfterUpdate()
   If Me.RiskAssesment = -1 Then
     Risk.Visible = True
   Else
     Risk.Visible = False
   End If
End Sub

Private Sub Form_Current()   
  If Me.RiskAssesment = -1 Then
    Risk.Visible = True
   Else
     Risk.Visible = False
   End If
End Sub
Notice that I have RiskAssesment written with one s. This is misspelled, in English, but I left it as it is given in your example. The important thing is that it is spelled the same way everywhere!

Linq ;0)>
__________________
Hope this helps!

The Devil's in the Details!!

All posts/responses based on Access 2000/2003
Reply With Quote
  #3 (permalink)  
Old 01-29-12, 13:51
oMADMANo oMADMANo is offline
Registered User
 
Join Date: Jan 2012
Posts: 16
The code you provided does now does not work

i have also tried
Code:
Private Sub RiskAssesment_Click()

 Set db = CurrentDb
 Set rs = db.OpenRecordset("tblMembership_Database", dbOpenDynaset)

 If rs.RiskAssesment = False Then


rs.Risk.Visible = False
rs.Risk1.Visible = False
rs.RiskText.Visible = False
rs.Risk1text.Visible = False
rs.RiskButton.Visible = False

 ElseIf rs.RiskAssesment = True Then

rs.Risk.Visible = True
rs.Risk1.Visible = True
rs.RiskText.Visible = True
rs.Risk1text.Visible = True
rs.RiskButton.Visible = True
rs.Update

End If

End Sub
with the name of my table being Membership Database.
It gives me the error cant find table/query Membership database.
Reply With Quote
  #4 (permalink)  
Old 01-30-12, 12:22
Sam Landy Sam Landy is offline
Registered User
 
Join Date: May 2004
Location: New York State
Posts: 931
Mind if I comment?

Remove the code from the Sub RiskAssesment_Click() event altogether. The only code you need is in the Form_Current() event. Add the code there as follows:

Code:
rs!Risk.Visible = Me.RiskAssessment
As Missinglinq pointed out, make sure you spell it consistently. Of course you have to open the table there in order to access the data. You also need the same code in the Sub RiskAssesment_Update() event.

However, I'm confused about something altogether different. Your VBA code is accessing rs!RiskAssesment, etc., but there's no code pointing to a record. Your cursor is still at BOF() (beginning of file), and is not pointing to any data at all.

Also, the code
Code:
If rs.RiskAssesment = False Then


rs.Risk.Visible = False
rs.Risk1.Visible = False
rs.RiskText.Visible = False
rs.Risk1text.Visible = False
rs.RiskButton.Visible = False

 ElseIf rs.RiskAssesment = True Then

rs.Risk.Visible = True
rs.Risk1.Visible = True
rs.RiskText.Visible = True
rs.Risk1text.Visible = True
rs.RiskButton.Visible = True
rs.Update
is almost completely redundant. All you need is
Code:
rs.Edit (which you also don't have anywhere in the presented code snippet)
rs!Risk.Visible = rs!RiskAssesment
rs!Risk1.Visible = rs!RiskAssesment
rs!RiskText.Visible = rs!RiskAssesment
rs!Risk1text.Visible = rs!RiskAssesment
rs!RiskButton.Visible = rs!RiskAssesment
rs.Update
Dataset fields are delineated by "!", not ".", so you should have, for example,
Code:
rs!Risk.Visible = rs!RiskAssesment
"." is used to delineate methods (such as ".Update") or properties (such as ".Visible"), not field names.

Also, the whole snippet looks suspicious to me. Are using "rs." to refer to the form, and not the dataset? The form object is identified by "Me", not by the table object's name.

By the way, what is the real name of the table? Using reserved words such as "Database" as part of an object's name is just begging for trouble. Access has its own meaning for those words, and couldn't care less how the programmer means it.

Good luck,

Sam
Reply With Quote
  #5 (permalink)  
Old 01-30-12, 12:54
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
if you put the code in the on current event I thought that only triggered when a new record is displayed, either an existing one that is being edited or a new one just added granted iof you want to display that information on histroic records then thats a good solution

however if a user clicks a button to toggle the display on or off then you need code behind that button
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 01-30-12, 16:35
Sam Landy Sam Landy is offline
Registered User
 
Join Date: May 2004
Location: New York State
Posts: 931
Both true, and I based my answer on what looks to me like a historical table, not where he's adding records.

Sam
Reply With Quote
  #7 (permalink)  
Old 01-31-12, 04:21
oMADMANo oMADMANo is offline
Registered User
 
Join Date: Jan 2012
Posts: 16
The problem I am having now is the risk , risk1 , risktext , risk1text , risk button are not data fields in my "membership database" they are controls on my form.
so I am still having trouble with this.
Reply With Quote
  #8 (permalink)  
Old 01-31-12, 05:00
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Then use expressions such as:
Code:
Me.Risk.Visible = Me.RiskAssessment.Value
__________________
Have a nice day!
Reply With Quote
  #9 (permalink)  
Old 01-31-12, 11:14
oMADMANo oMADMANo is offline
Registered User
 
Join Date: Jan 2012
Posts: 16
I have tried the Me. expression but it applies the code to all records. I only want to make the objects visible on the current record if the tick box is selected.
Reply With Quote
  #10 (permalink)  
Old 01-31-12, 13:01
Sam Landy Sam Landy is offline
Registered User
 
Join Date: May 2004
Location: New York State
Posts: 931
I always default the display to a Single Record. That solves problems such as this.

Sam
Reply With Quote
  #11 (permalink)  
Old 01-31-12, 13:49
oMADMANo oMADMANo is offline
Registered User
 
Join Date: Jan 2012
Posts: 16
Could you possibly explain how you do that please.
Reply With Quote
  #12 (permalink)  
Old 01-31-12, 13:54
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
select the forms properties
switch from continuous forms to single form
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #13 (permalink)  
Old 01-31-12, 14:13
oMADMANo oMADMANo is offline
Registered User
 
Join Date: Jan 2012
Posts: 16
If you mean default view as single form this doesn't work the objects are still visible on all records.
Reply With Quote
  #14 (permalink)  
Old 01-31-12, 14:49
Sam Landy Sam Landy is offline
Registered User
 
Join Date: May 2004
Location: New York State
Posts: 931
Are these objects part of the form, or are they actually stored in the record in the table?

Sam
Reply With Quote
  #15 (permalink)  
Old 01-31-12, 14:54
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
my reading is the OP is using continuous forms and trying to apply logic to a single row, which as you correctly point out / infer cannot be done.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On