Add column named current date - vba

I'm creating an Access database to try to track average cost of inventory. I want to make it to where when a button is clicked on the form a column is added to the table that is named with the current date and the word Quantity and another column is added with the current date and the word Price. I can't just add columns named quantity and price because I am hoping to be able to use this over time. So I want to know on mm/dd/yyyy we bought x widgets for y dollars a piece. Then a week later I want to be able to hit the same button and add more lines with the new current date. Is this even possible? I'm using Microsoft Access 2016. I've tried the following with some variations based on what I've researched but nothing seems to work. AvgCost is the table name. Also I'm very very new at this.
Private Sub Record_Button_Click()
ALTER TABLE AvgCost ADD COLUMN Quantity_DATE()
End Sub
I keep getting an error message reading "Compile Error: Expected: ENd of Statement and it highlights AvgCost

Can't run an SQL action in VBA like that. Certainly won't create a field with a date in field name. Need to call a command and concatenate date value.
CurrentDb.Execute "ALTER TABLE AvgCost ADD COLUMN Quantity_" & Format(DATE(), "yyyymmdd") & " Number"
However, this really is poor design. Instead of implementing procedure that routinely changes database structure, should be adding records.

Related

SQL to set a date and constant number

So I am creating a database to track projects. In it I have several milestones. On the form each milestone has a label and a textbox next to it. The project manager will go in and type the date of that milestone.
What I need to do is based on the textbox that date is typed in it will save date and milestoneID to a table.
If you look at the Form and Table images attached:
You see on the form they enter the date next to milestone.
On the table you see the each date gets a schedule ID (autonumber), the projectID gets assigned to it (another table), and then the milestoneID(phaseID) followed by the StartDate. Ignore the end date. I need that schedPlanPhaseID to fill in based on which text box then entered a date in on the form.
I am still very new to SQL and access so sorry if I miss something obvious.
Form Image
Table Image

Increment a number by 4, starting with a number I choose

Is it possible, if one does not want to take the AutoValue, to take as default value for a Large Integer, or even normal number, the largest value already used for this from the column and add to it?
I need an increment of 4, starting with a number I choose.
I'm using version 2016 of Office.
I found several possible solutions, which I've entered into the expression generator of the default value at Field Properties:
CODE: SELECT ALL
=Max([MyID])+1
Or
CODE: SELECT ALL
=DomMax("MyID"; "Table name")
I always get the error message:
Unknown function 'DMax' in the validity expression or default value in 'MyID.TableName'
I came across the tip that the references under VBA Editor --> Extras --> References must be correct. Here, however, I haven't the slightest idea which of these I would have to activate, if this is the problem at all.
The PrimaryKey of a table can sometimes be used as a part number in a Parts table or a userID in a Users table but otherwise is a number the user of a database should never see and certainly not manipulate. This is the first reason I suggest solution 2.
Solution 1. In Access just like with any other database I have seen that uses SQL, You can create an AutoNumber that increments by 4 using the Data Definition Language part of SQL. Maybe the Access designers considered changing the increment too rare or too advanced to put in the menu system. However, Altering the increment is not hard just open the query designer and go to the sql tab and type in appropriate SQL.
In Access 2007, how do I change the Auto Increment value?
First, Create your table as normal but don't insert any data. Then open the query designer, and open the sql pane (In this case it doesn't matter what you have in the design view as you are over writing any SQL), type
ALTER TABLE MyTableName
ALTER COLUMN MyAutoNumberColumnName AUTOINCREMENT(1,4);
The sql pane's ribbon should auto select Data Definition Language.
Run the query and solution 1 is done. When you enter Data in the table MyAutoNumberColumnName will start at 1 and increment by 4.
Solution 2: Don't use the primary key. Instead create another variable and display it. This is one example why data should be entered using forms. In the form's BeforeInsert Event calculate the variable and set it's textbox.
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.txtBoxAlternateIDColumn.Value = Nz(DMax("MyAlternateIDColumn", "MyTableName") + 4, 1)
End Sub
'NZ handles the annoying case where the table is completely empty
I've used Solution 2 a couple times and I don't let the person doing data entry even see the alternateID textbox on the data entry form.
You can also use a Create Table statement in the DDL
Here is a youtube example of solution 2: https://www.youtube.com/watch?v=ZOg4P6v5ewA

Matching VBN Textbox Text with Autonumber With Custom Format in Access

I have an access database where "Orders" is my table with the column name CusID and is set to Autonumber with format "CUS"0001
I'm trying to read an autonumber with the custom format "CUS0001" from VBN but I can't seem to read it.
I've tried to read it all as a string, but I can't seem to read it.
cmdCustomer.CommandText = "Select * From Orders Where CusID = " & (txtCusID.Text) & ";"
Any help would be greatly appreciated! Thank you :)
As the name suggests, AutoNumber values are numbers. "CUS0001" is clearly not a number so clearly cannot be stored in that column. When you specify a format in Access, that relates ONLY to how the Access application displays that data. It says nothing about how the data is stored. If Access displays a value in that column as "CUS0001" then the column actually contains the number 1 and that is all that your VB app will see, so that's how you have to query it. Also, if you want the value displayed as "CUS0001" in your app then YOU are going to have to format it that way.
It's also worth noting that, if you really did want to search for "CUS0001" then you'd have to wrap that value in single quotes in your SQL code or else you're going to get a syntax error. That said, it shouldn't matter because you should be using a parameter to insert that value into your SQL code.

MS Access Delete query based on combobox

I have a table with a bunch of different fields. One is named period.
The period is not part of the raw data but I run a query when I import new data to the database that gives each record a period.
Now I need a delete query that will delete all the records that have the same period as what is selected in a combobox.
The values in the combobox come from a calendar table that contain all the possible values that could be in that period column at any time.
This is the basic query i thought would solve this issue but it tells me it is going to delete 0 rows every time I run it:
DELETE *
FROM PlanTemp
WHERE PlanTemp.period = Forms![Plan Form]!Combo163;
If you don't need the key field, just remove it.
Look at the "PROPERTIES" section and look at the column names.
Ether remove it there, or from your QUERY source.
You can also look at the Data section of the properties, and change your BOUND column, to Column 2... or whatever holds the data you want to use.

Save a calculated result in a table using a query (Access 2007)?

I have a form of multiple project sites where one of the fields is labelled "Project Start Date". I have another field in the form labelled "Projected Project Finish Date". Both fields are stored in a table labelled "General Project Info" (well, at least that's the source of the information for the form come from).
I could manually fill in the Proposed Project Finish Date by adding 10 days to the Project Start Date, but I would like to make it a calculated field, i.e once someone puts in the Project Start Date Access automatically calculates the Proposed Project Finished Date.
I am aware you can use a query to calculate this: New Query-->Include Project ID and Project Start Date fields, then make the third field--> Proposed Finish Date: DateAdd("d",10,[Project Start Date]). This produces a query result which has a column with all the sites, a column with the Project Start Date and a column with a date that is 10 days later. Perfect. But, how do I store those new results in my existed General Project Info table and have them appear in the form? I'm obviously a beginner and am missing something.
Thanks for any help in this matter.
Oh, I am using Access 2007.
You can use the After Update event for Project Start Date on your form to update the Project End Date control to Project Start Date + 10, but if the answer is always + 10, why store the date at all? Just use your query to show the end date.
Private Sub StartDate_AfterUpdate()
Me.EndDate = Me.StartDate + 10
End Sub
Or
SELECT StartDate, StartDate + 10 As EndDate FROM ATable
Edit
To add an After Update event, use the property sheet in form design view. Select [Event Procedure] and then click the three little dots. It will open up the code window and you can add code into the event. You will need to use the proper names for you controls, but if you type Me., intellisense will help you along with names of properties, methods and controls.