473,396 Members | 1,764 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,396 software developers and data experts.

Adding a field to existing table in access using vba

Ok so I am working on a Access project and I have hit a roadblock and I need your expert help.

I have a table(tblWorkerStats) with fields: [Worker Code], [worker name], [Jan '16], [Feb '16], [MAr '16], [Apr '16]. All the month name fields contain a count of cases that the worker has not processed in time for that month. I have another table (tblOverdue) with fields: [worker code], [case number] which lists all the individual case numbers that were not processed in time for that current month. We get this monthly data from a remote server and only provides data for the current month.

So what I am trying to do is populate a new field in (tblWorkerStats) each month to store the count of cases per worker for the current month. How would I go about doing that?
I do not think I can use a APPEND or UPDATE query. What I thought of doing is call on VBA event procedure on the form to store the data from (tblWorkerStats) into rs1 as recordset. And store results from (qryCountOverdue) into rs2 as recordset.

Now I want rs2 data appended to rs1 joined on [worker code]. Also if possible, I want the new field name to be the current month name.

And another complex problem is that on the report is that each month I only want to show stats for the last 3 months and since the field names of the last 3 months are going to change every month, I do not know how I would be able to accomplish that. Maybe display last 3 fields based on the field location ordinals but I do not know how.

P.S. I am very new to Access and VBA but I'm catching up very quick and with your help I'm hoping I'll learn a lot more.

Thank you
May 11 '16 #1
13 1400
If I understand your question correctly you have several repeating Fields:- [Jan '16], [Feb '16], [MAr '16], [Apr '16]...

I suspect this is the essence of your problem. This isn't the way you would do things in MS Access this is more like the way you would do them in Excel.

You inadvertently pointed out the problem yourself in your statement "And another complex problem is" The data in each of those Fields is of the same sort, therefore it should be stored in one field in a table with another field to identify the data. Once you grasp this, you will find things become much easier, you will use queries efficiently and effectively to get the results you need.

So to sum up a rather long answer, you need to change your database structure. The sooner you change it the better.
May 11 '16 #2
Thank you for your response Tony.

I understand the structure is not the best, however we get data from a remote server for current month only. And the data we get is a list of cases every month and what I'm interested in is only storing the count of cases each month.

So what you are suggesting is I create a separate table with fields, worker code, count of cases and month and i'll identify the count with the month. That makes sense to me and I probably could accomplish that through a APPEND query every month and assign value to the month field with format(Now(),"mmmm\'yy"). Am I correct? Would I be able to use aggregates in a Append query since all I need to store is the count of cases per worker and not the whole list of cases.

Also,on the report, how would I show 3 separate columns for the last 3 months where data is coming from the same field? I am not able to think that part logically.

Thanks again.
May 11 '16 #3
I still think you need to change your structure. You are getting the data in per month, count per user.

Let's say you're next month is [May '16]
You have 5 user --- ID's 1,2,3,4,5
Counts of --- 43,65,23,87,12
Instead of adding that data into a new field, which is something you should never really consider, then you add the data to fields in a table. The table structure would look like this:-

fldMonth --- fldUser --- fldCount
[May '16] --- 1 -------------43
[May '16] --- 2 -------------65
[May '16] --- 3 -------------23
[May '16] --- 4 -------------87
[May '16] --- 5 -------------12
May 11 '16 #4
As to the report, how about three subreports which display each month next to each other?

The SQL for each subreport could be written so that it would automatically pick up the data for the last 3 months.
May 11 '16 #5
Thanks again Tony. I said exactly what you are suggesting but I guess I didn't word my explanation correctly.
I modified my current structure and split my current table into 2 tables.
tblWorkers
[Worker Code] [Worker name] [location]
a1----------- john doe----- california
a2----------- jane doe----- utah
b1----------- john smith--- texas

tblStats
[worker code] [overdues] [month]
a1------------25-------- apr
a1------------15-------- may
a1------------27-------- mar
a2------------52-------- may
a2------------36-------- apr
a2------------12-------- mar

Now what my logic tells me is when I run the countcases query each month, I should make that a append query that appends to tblStats and the value of the month field would be Format(now(),"mmm"). Is my logic correct so far?
If it is correct, I still cant logically think I would I get my report to look like
[Worker Code] [worker name] [mar] [apr] [may]
a1------------John Doe-------27----25----15--
a2------------Jane Doe-------12----36----52--

And next month I dont want to redesign the report but next month report should show
[Worker Code] [worker name] [apr] [may] [jun]
a1------------John Doe-------25----15----xx--
a2------------Jane Doe-------36----52----xx--

I hope I was more clear now. Thank you again for sparing the time to help me out. I really appreciate it.
May 11 '16 #6
[Worker Code] [worker name] [apr] [may] [jun]
a1------------John Doe-------25----15----xx--
a2------------Jane Doe-------36----52----xx--

Why is the worker code different?

Opps --- I see i read Jane as "John"
May 11 '16 #7
Also, going by the above flow, I might face another problem with the APPEND query. I do not want the query to execute if it has already been executed for the current month. i.e. if there is [month] field already has Format(now(),"mmm") value then abort query. I think I might need to use VBA to achieve that. Any ideas?
May 11 '16 #8
zmbd
5,501 Expert Mod 4TB
1) IMHO: You need to take a look at, Database Normalization and Table Structures

Your tblWorkers might be able to be taken apart one more level depending on the actual usage for location.
-- Can a Worker be in more than one location?
-- COULD the Worker EVER be in multiple locations in the future? If this could EVER be true then you should split this out!

I tend to pull the months in to a separate table, That way were not saving "January" over and over again.

2) You really shouldn't store your calculated values. Instead of a tblStats you should have an aggregate query qryStats. The query takes your data, filters on the date range etc...

>> This solves your Append-Query issue :-)

Queries are your friends!
Normally, calculated results are not saved.
> Exception 1: Legal requirement.
> Exception 2: Based on a variable data point such as Tax or Discount rate that might change. (HOWEVER, I would store the rate and use a related table)
> Exception 3: The calculated value does not change except under very controlled conditions. CAUTION HERE, there's usually not much to be gained unless the data set is HUGE and/or the calculation is very complex - hence the speed increase is worth the potential risk of using a wrong value.
> Exception 4:... well... I don't have another exception; however one of the other Experts may have one or two more.

3) Your final report could be based on a crosstab query, the CTQ would be fed by your qryStats. Where [WorkerCode][WorkerName] are row headers, Months would be the Column header, and Stats the data field. The wizard does a decent job on simple CTQ and Allen Browne has a nice article covering CTQ : AB: Crosstab query techniques. Cross tabs are a weak-point in my knowledge and Allen's article has helped point me in the right direction a few times.

Once the query is made and showing the required data, the report can be formatted to your requirements.
May 11 '16 #10
jforbes
1,107 Expert 1GB
You could use a Crosstab Query for your running totals of the last three months. But I don't think it would be the best fit here. Crosstabs will generate columns based on your data so the amount of columns and their names can vary based on the dataset being selected. Since you have already defined the columns you want to show on your report, and they wont vary, I would create a normal query and include a running totals for each of the last three months for each of your records. You could do this with Inline Queries or create a Aggregate Query to perform the Sum by month and then Join to it.
May 12 '16 #11
Thank you guys. Appreciate all the input and suggestions.

@zmbd : Our organization structure is such that the [Worker Code] is going to be associated with the location. Fro example, [worker code] a1 is always going to be California location. The [Worker Name] associated to that worker code can fluctuate on a regular basis (sometimes monthly). I included the location column in that table so when I am doing the reports, I can do individual reports pertaining to each location. And I didn't think a crosstab query would be a feasible solution (note that I'm not familiar with CTQ).

@jforbes : I was thinking in line with that you suggested after I implemented zmbd's original suggestion of normalising and splitting the table. Only thing now is that I'm confused as to using a APPEND query to the tblStats to store all overdues for all months in the same table or to use a MAKE TABLE query each month to store the Overdue numbers for that particular month.

I understand that storing a calculated field is against the norm but in this particular scenario I cant see a solution that avoids storing the count each month in a table because it'll be new data each month and if I do not store the count in a table, I would lose it.
May 12 '16 #12
Also, if I use a APPEND query, may be I can use the following SQL statement:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblWorkerOverdues ( Unit, Overdues, [Month] )
  2. SELECT tblWorkers.Unit, Count(tblOverdue.Case) AS Ovedues, Format(Now(),"mmm") AS [Month]
  3. FROM tblOverdue RIGHT JOIN tblWorkers ON tblOverdue.Unit = tblWorkers.Unit
  4. WHERE Format(Now(),"mmm") NOT IN (tblWorkerOverdues.month)
  5. GROUP BY tblWorkers.Unit;
The problem with the above query is that it asks the user input for tblWorkerOverdues.Month.

P.S. tblOverdue is the table with all the case data we get every month. tblWorkers is the list of all workers and tblWorkerOverdues is the table that stores all monthly statistics.
May 12 '16 #13
Thanks to Tony Hine, Zmbd and jforbes for helping me with your valuable pointers and suggestions. I have accomplished what I wanted. Here is what I did:
APPEND Query to add to the tblWorkerOverdues:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblWorkerOverdues ( Unit, Overdues, [Month] )
  2. SELECT tblWorkers.Unit, Count(tblOverdue.Case) AS Ovedues, Month(Now()) AS [Month]
  3. FROM tblOverdue RIGHT JOIN tblWorkers ON tblOverdue.Unit = tblWorkers.Unit
  4. WHERE (((Month(Now())) Not In (SELECT month FROM tblWorkerOverdues)))
  5. GROUP BY tblWorkers.Unit;
Then I created a CTQ. It took a lot of googling to finally achieve the result but I enjoyed the learning process. CTQ code:
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Avg(Query1.Overdues) AS AvgOfOverdues
  2. SELECT Query1.SP, Query1.Wkr, Query1.[Worker Name], Avg(Query1.Overdues) AS [2016 Monthly Average]
  3. FROM Query1
  4. GROUP BY Query1.SP, Query1.Wkr, Query1.[Worker Name]
  5. PIVOT Query1.Month-2 In (1,2,3);
  6.  
I had to use IN statement to have a consistent field name every month for the purpose of the report.

And finally on the report I just had to use Format() and DateADD() to name the fields to last 3 months:
Expand|Select|Wrap|Line Numbers
  1. =Format$(DateAdd("m",-2,Now()),"mmm")
Expand|Select|Wrap|Line Numbers
  1. =Format$(DateAdd("m",-1,Now()),"mmm")
Expand|Select|Wrap|Line Numbers
  1. =Format(Date(),"mmm")
And Voila, I have my report. Thanks again everyone. :)
May 12 '16 #14

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

Similar topics

2
by: Gert Schumann | last post by:
I want to delete just parts of tables, so I can't use 'TRUNCATE'. As I want to delete about millions of lines, I need a very big rollback segment. The best way would be to delete without using...
11
by: Bobbak | last post by:
Hello All, I have these tables (lets call it ‘EmpCalls', ‘EmpOrders', and ‘Stats') that each contain the list of EmployeeIDs, I want to be able to create a Module in which I could call in my VB...
3
by: GL | last post by:
Hi, Is there a way to add a field to an existing table using a query of some sort (without needing to manually add a field to the table). I know how to do it with a make table query, but I have...
1
by: Randy | last post by:
I built and employment report from the employment table. I then had to add a field to the table. I now need to add that field to the report. I can't seem to be able to do that by clicking on...
0
by: Bennett Haselton | last post by:
It seems whenever I add a new field to a database table that's queried by DataAdapters in my project, I get run-time errors because I don't go back and re-generate all the DataAdapters to read the...
5
by: Przemek Wrzesinski | last post by:
Hi, I'm trying to add additional column using 'alter table' command via OleDB to Excel workbook (one sheet called queExportBOND): Dim strConn As String strConn =...
5
by: Bubo.virginianus | last post by:
Heya! I feel I'm beating an old horse to death with this question but I can't get around the problem. I want to update a text field on a form with a string from a VB module. I need...
2
by: acw | last post by:
On a SQL Server 2000 db I would like to setup a stored procedure that accesses couple tables and runs the extended stored procedure xp..cmdshell. The goal is to grant users with limited privileges...
1
by: Tyrone000 | last post by:
Hi, Good day! I would like to ask for a code. Im doing a Daily Time Record system in VB 6.0 the problem is when i time in on the next day, it's debugging. it wont add new record since it has...
0
by: KyzPu | last post by:
Hi there! I am making a database and I want to add columns to an existing table (tbl1) using the records from another table (tbl2) with VBA. For example: the first added column (tbl1) should be...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.