473,395 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reference a cell in an access table

33
Hi y'all
Can I reference a cell in an access table within a VBA code?? I have 1 x 2 table (1 row and 2 columns). The first column is just an autoID generator. The second column keeps track of dates. I want to write a VB code that changes the table record in row 1, column 2 of the table, tblDate. The field name is ChngDate

Here's my code:
Expand|Select|Wrap|Line Numbers
  1. Sub SetDate()
  2. Dim CellDate As Date
  3. ' Assign data in cell (row 1, col 2) to CellDate
  4. CellDate = DateValue(tblDate.ChngDate)
  5. ' Add 3 months to the CellDate
  6. CellDate = DateAdd("m", 3, tblDate.ChngDate)
  7. ' Assign the new value back to the Table
  8. tblDate.ChngDate = CellDate
  9. ' Begin loop when date in table cell (row 1, col 2) = current system date
  10. Do While CellDate = Date
  11.          ' Add 3 months to current date in table cell (row 1, col 2)
  12.          CellDate = DateAdd("m", 3, Date)
  13.          ' Assign the new value back to the Table
  14.          tblDate.ChngDate = CellDate
  15. Loop
  16. End Sub
Jul 29 '08 #1
10 20716
owuraku
33
Hi y'all
Can I reference a cell in an access table within a VBA code?? I have 1 x 2 table (1 row and 2 columns). The first column is just an autoID generator. The second column keeps track of dates. I want to write a VB code that changes the table record in row 1, column 2 of the table, tblDate. The field name is ChngDate

Here's my code:
Expand|Select|Wrap|Line Numbers
  1. Sub SetDate()
  2. Dim CellDate As Date
  3. ' Assign data in cell (row 1, col 2) to CellDate
  4. CellDate = DateValue(tblDate.ChngDate)
  5. ' Add 3 months to the CellDate
  6. CellDate = DateAdd("m", 3, tblDate.ChngDate)
  7. ' Assign the new value back to the Table
  8. tblDate.ChngDate = CellDate
  9. ' Begin loop when date in table cell (row 1, col 2) = current system date
  10. Do While CellDate = Date
  11.          ' Add 3 months to current date in table cell (row 1, col 2)
  12.          CellDate = DateAdd("m", 3, Date)
  13.          ' Assign the new value back to the Table
  14.          tblDate.ChngDate = CellDate
  15. Loop
  16. End Sub
  17.  
PS: tblDate.ChngDate has a starting value of "6/30/08". And I have modified the code from previous post to make question a lot clearer.
Jul 29 '08 #2
janders468
112 Expert 100+
If your table is static, i.e. you won't be adding more rows to it, then you can just use SQL to update the date column as there is only one value to update that will be the one that gets updated. You can run the query from vba if you want it to be a vba procedure. You could also use recordsets which give you a way to reference specific row/column positions (essentially the same idea as a cell).
Jul 29 '08 #3
owuraku
33
If your table is static, i.e. you won't be adding more rows to it, then you can just use SQL to update the date column as there is only one value to update that will be the one that gets updated. You can run the query from vba if you want it to be a vba procedure. You could also use recordsets which give you a way to reference specific row/column positions (essentially the same idea as a cell).
Thanks Janders, I had thought of recordsets and SQL too. With recordsets, I havent really worked with them enough to be able to implement them into my code. As for SQL, can you tell me how I can incorporate the "DO WHILE" loop into it? I know I can do a regular UPDATE query but my problem has to do with the loop...how do I loop in a query??

PS: And yes. My table is STATIC!
Jul 29 '08 #4
janders468
112 Expert 100+
I am trying to understand how your loop is operating, but it appears that you are telling it to update the table value as long as the date in the table value is equal to today's date. Is this correct? It seems you might be viewing the table as a spreadsheet in design, when you run a query it will update every value in that column (one value in your case). If I've totally misinterpreted what you're saying I apologize.
Jul 29 '08 #5
owuraku
33
I am trying to understand how your loop is operating, but it appears that you are telling it to update the table value as long as the date in the table value is equal to today's date. Is this correct? It seems you might be viewing the table as a spreadsheet in design, when you run a query it will update every value in that column (one value in your case). If I've totally misinterpreted what you're saying I apologize.
Here's the break down of what the table's supposed to be doing:

At Today:
The value of cell = 6/30/08 + 3 mnths = 9/30/08

At 9/30/08 (Current system date at the time):
The value of cell = 9/30/08 + 3 mnths = 12/30/08
.
.
.
.
At nth day/30/mth year:
The value of cell = n/30/m + 3 mnths = (n+3)/30/m

From the above, the table would not be due for an update until 30th August. Basically until the entry date in the table equals the existing current date, table remains unchanged.

I hope it's clear enough.
Jul 29 '08 #6
janders468
112 Expert 100+
If my understanding is correct then you want to update this value to three months plus today if the date equals today's date, the below SQL will accomplish that. My table is called Test_tbl, and the Date Field is simply DateField. You can adjust it to the fields in your table and your table's name

UPDATE Test_tbl SET Test_tbl.DateField = DateAdd("m",3,[Test_tbl].[DateField])
WHERE (((Test_tbl.DateField)=Date()));

Let me know if that is what you are looking for.
Jul 29 '08 #7
owuraku
33
If my understanding is correct then you want to update this value to three months plus today if the date equals today's date, the below SQL will accomplish that. My table is called Test_tbl, and the Date Field is simply DateField. You can adjust it to the fields in your table and your table's name

Expand|Select|Wrap|Line Numbers
  1. UPDATE Test_tbl SET Test_tbl.DateField = DateAdd("m",3,[Test_tbl].[DateField])
  2. WHERE (((Test_tbl.DateField)=Date()));
Let me know if that is what you are looking for.
Thanks Bud...
That worked!! Guess I didnt need a loop function after all. Now another problem building off of this one is that I need to run a macro based off of the date in this table. Since the table data is going to change once every 3 months, I want to write a code that starts off the macro once every 3 months. So if the table data reads 10/30/08, the macro should run on this date; and the procedure should reference the table data.
Jul 30 '08 #8
janders468
112 Expert 100+
I didn't know if by macro you meant actual macro objects, but here is how I would approach the problem. To fire the event on open of the database you will either need to create a macro titled AutoExec (not trying to talk down if you already know all this) or from the tools...startup menu select an object to open on startup. If you choose the first option Access will run whatever is in the AutoExec Macro, if you choose the second you will have to place the relevant code in the OnOpen event of the object (probably a form) you choose to open. Either way the below function will return the result that you want.
Expand|Select|Wrap|Line Numbers
  1. Function CheckDate()
  2.     Dim dte As Date
  3.     Dim strSql As String
  4.     dte = DLookup("DateField", "[Test_tbl]")
  5.     strSql = "UPDATE Test_tbl SET Test_tbl.DateField = DateAdd('m',3,[Test_tbl].[DateField]) WHERE (((Test_tbl.DateField)=Date()));"
  6.     If dte = Date Then
  7.         DoCmd.RunSQL strSql
  8.     End If
  9. End Function
  10.  
Run CheckDate either from the AutoExec macro using RunCode and this function name or run it from the Open Event of the form.
I made this a function even though it doesn't return a value because Macros will not accept Subroutines, if you run it strictly from code it can be a Sub which would be more appropriate.
Jul 30 '08 #9
owuraku
33
I didn't know if by macro you meant actual macro objects, but here is how I would approach the problem. To fire the event on open of the database you will either need to create a macro titled AutoExec (not trying to talk down if you already know all this) or from the tools...startup menu select an object to open on startup. If you choose the first option Access will run whatever is in the AutoExec Macro, if you choose the second you will have to place the relevant code in the OnOpen event of the object (probably a form) you choose to open. Either way the below function will return the result that you want.
Expand|Select|Wrap|Line Numbers
  1. Function CheckDate()
  2.     Dim dte As Date
  3.     Dim strSql As String
  4.     dte = DLookup("DateField", "[Test_tbl]")
  5.     strSql = "UPDATE Test_tbl SET Test_tbl.DateField = DateAdd('m',3,[Test_tbl].[DateField]) WHERE (((Test_tbl.DateField)=Date()));"
  6.     If dte = Date Then
  7.         DoCmd.RunSQL strSql
  8.     End If
  9. End Function
  10.  
Run CheckDate either from the AutoExec macro using RunCode and this function name or run it from the Open Event of the form.
I made this a function even though it doesn't return a value because Macros will not accept Subroutines, if you run it strictly from code it can be a Sub which would be more appropriate.
Thanks Bud!!! You're gem. Although the code did not meet my exact need, it pointed me to where I wanted to get. I tweaked it a bit to serve my purpose. Here's my code:
Expand|Select|Wrap|Line Numbers
  1. Function RunMacro()
  2. Dim dte As Date
  3. dte = DLookup("[RecertDate]", "tblRecertDate")
  4. If dte = DateValue(Now()) Then
  5. DoCmd.RunMacro "RecertMacro"
  6. End If
  7. End Function
  8.  
Jul 30 '08 #10
NeoPa
32,556 Expert Mod 16PB
FYI Date() returns exactly the same value as DateValue(Now()).
Aug 5 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Richard | last post by:
Hi I am trying to place an e-mail link into a cell of an Access 2002 table. Have tried setting the cell as both a hyperlink and as plain text and putting the following into the box: <a...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
2
by: Kenneth D Buelow | last post by:
Is there a way to generate a listing of some/all of the "field descriptors" for an ACCESS table? I am a SAS user and am looking for something like SAS's proc CONTENTS which generates a basic "data...
7
by: parag1234567 | last post by:
Hi, I am dynamically generating a html file which will contain only <div> tags which contents are hidden from user( set by style="visibility:hidden") Now the next step is i am enabling some of...
1
by: SteveBark | last post by:
Hello all I am currently trying to develop a script that will take a value from an Excel spreadsheet cell and use that to run a query against an Access table to delete all rows that match the...
4
by: bdockery | last post by:
Access 2007 I have a table with three columns. Country, State, City What I want to do is have a form where the Country, and State, lookups auto-populate when the city lookup is chosen. It...
1
by: Evert | last post by:
Hi all, I am stuck and I need some help. The idea is to automatically collect data from an Excelsheet report that is being distributed multiple times per day. In this report there are only...
1
by: JFKJr | last post by:
Hi! I have exported an access table data into an excel file using the following code. However, the code exported all the data to the excel sheet starting from 'A' cell (first column). But I want to...
6
by: JFKJr | last post by:
Hello everyone, the following is the Access VBA code which opens an excel spreadsheet and creates combo boxes dynamically. And whenever a user selects a value in a combo box, I am trying to pass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.