Trying to insert a set number of rows into a child table based on a number entered into a parent table
parent (licenseID, poolSize)
child (nodeID, licenseID)
I was looking into using an After Insert trigger on the parent table but could not figure out how to loop a create record command.
Is this possible to do with macros or should I be looking at VBA code? (MS ACCESS 2010)
First of all, I would never enter anything directly into a table. Giving your users that kind of ability is always going to end up in some form of misery and regret.
I would creat a form to take the number, and then use code behind a "Submit" button to loop the required number of times. Something like (this code is entirely untested):
Dim db as Database
Dim rec as Recordset
Dim X as Double
Set db = CurrentDB
Set rec = db.OpenRecordset ("Child")
Do for X = 1 to Me.txtNumberOfEntries
rec.AddNew
rec("LicenseID") = me.txtLicenseID
rec.Update
Next X
'Always clear out your variables
Set rec = Nothing
Set db = Nothing
Related
Looks like in previous versions you could use ##nestlevel, but in ver 12 it always returns -1.
So how do you avoid update loops?
I'm trying to sync multiple connected contacts in the same table.
ALTER TRIGGER "Sync" AFTER UPDATE
ORDER 1 ON "Contact"
REFERENCING OLD AS oldRec NEW AS newRec
FOR EACH ROW
BEGIN
IF ##nestlevel = 1 THEN
UPDATE Contact set Title = newRec.Title
WHERE newRec.syncid = Contact.syncid;
END IF
END
This results
Is there any way to add a column into an MS Access database using VBA and SQL query and then insert values using formulas from other column.
As an example, I want to create a column employee and it should have calculated values on all the records. I want to use LEFT(col2, 3) in the employee field.
This is my code:
Sub createcolumn()
Dim objAccess As Object
Set objAccess = CreateObject("Access.Application")
Call objAccess.OpenCurrentDatabase("H:\VBA\Array Practice\Database1array.accdb")
objAccess.CurrentProject.Connection.Execute ("ALTER TABLE Company ADD COLUMN employee CHAR ")
End Sub
Sub insertvalues()
Dim objAccess As Object
Set objAccess = CreateObject("Access.Application")
Call objAccess.OpenCurrentDatabase("H:\VBA\Array Practice\Database1array.accdb")
objAccess.CurrentProject.Connection.Execute "INSERT INTO Employee (Gross) VALUES LEFT([full_name], 3)"
End Sub
I am able to create a new column but could't insert the value.
You can directly update the table using column FULL_NAME instead of inserting. I have tested the following code and it is working.
Sub test()
Dim dbs As Database
Set dbs = OpenDatabase("Database1.accdb")
dbs.Execute "UPDATE EMPLOYEE SET GROSS=LEFT(FULL_NAME, 3)"
dbs.Close
End Sub
You added a column to a table named Company, but you are attempting to insert values to a table named employee.
If the table named company as records then an update statement is what you need to add values into the new column employee, but if the table does not have any record, then an insert statement can be used to add values into the employee field.
I am creating a simple database. I have a functionality where i have simply created a Insert Into query an saved it in the database. what i want to do is,
call that query in VBA code.
I have done following coding :
Private Sub Update_ISO_Review_Register()
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Execute "Update_ISO_Review_Register_ApplicationData"
dbs.Close
Set dbs = Nothing
End Sub
The code works and it executes the query. Issue is that after that it locks the database and it gives the following error.
You do not have exclusive access to the database at this time. if you
proceed to make changes, you may not be able to save them later.
The SQL for the Update_ISO_Review_Register_ApplicationData query is:
INSERT INTO ISO_REVIEW_REGISTER ( SLTF_Ref, Brand, Application_No )
SELECT DISTINCT b.matter_No AS SLTF_Ref
, b.Brand
, b.CREDIT_APPLICATION_ID AS Application_No
FROM WBC_HFM_FileReveiw_Table AS a INNER JOIN WBC_HFM_Application_Table AS b ON
a.CREDIT_APPLICATION_ID = b.CREDIT_APPLICATION_ID;
Any help ?
Would it work if you only do this:
Private Sub Update_ISO_Review_Register()
CurrentDb.Execute "Update_ISO_Review_Register_ApplicationData"
End Sub
You can nicely live without these:
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Close
Set dbs = Nothing
especially set set dbs = Nothing, which is a local variable and "dies" after End Sub.
Say i have a table in Excel:
TableA:
ID,
Name,
Work,
DOB
I have an insert query like:
Insert into TableA(ID,Name,Work,DOB) values (....);
I have created the code to upload this data into SQL Server database using ADODB
The upload works fine but i am trying to have it fill the status of the upload such (Pending, Successful) next to each row. Is there any way to do it?
The ending part of my code is:
For lp = 0 To qryindex - 1 'this loop is used to get query from the array which is stored in previous loop
con.Open 'open the connection
con.Execute qryary(lp), dbfailonerror ' execute the query one by one according to index value
oldValue = Application.ActiveCell.Value
con.Close
Next lp
MsgBox "Data Updated Successfully"
ThisWorkbook.RefreshAll
I need it to update the coulmn(F) as each query in the array is successfully posted
Is it possible to list all triggers in all tables on a database?
I can list all user stored procedures with:
Private Sub StoredPro()
Dim theServer As Server = New Server(serverName)
Dim myDB As Database = theServer.Databases("mydatabase")
Dim dt As DataTable = myDB.EnumObjects(DatabaseObjectTypes.StoredProcedure)
End Sub
But nothing obvious in SMO for triggers. (I'm actually trying to list just custom triggers, any that are not named DEL_tablename, INS_tablename, UPD_tablename) within the selected database.
https://social.msdn.microsoft.com/forums/sqlserver/en-US/bb024fd6-c0b9-441a-864e-a579fc441267/list-databasetable-triggers-via-smo
A DatabaseDdlTrigger object represents a data definition language (DDL) trigger, which is created on a SQL Server database. However, from your description, I think you want to iterate over the data manipulation language (DML) triggers, which are created on SQL Server tables.
Please refer to the following sample:
Dim srv As New Server
Dim db As Database = srv.Databases.Item("Northwind")
Dim tb As Table
For Each tb In db.Tables
Dim trg As Trigger
For Each trg In tb.Triggers
If (((trg.Name <> ("INS_" & tb.Name)) AndAlso (trg.Name <> ("DEL_" & tb.Name))) AndAlso (trg.Name <> ("UPD_" & tb.Name))) Then
MessageBox.Show(trg.Name)
End If
Next
Next
Each database object has a Triggers property, which you can iterate to find all triggers.
Once you have a reference to this trigger, check the IsSystemObject property to check if it is a user-defined trigger or not.
foreach ( DatabaseDdlTrigger oTrigger in oDatabase.Triggers) {
if (! oTrigger.IsSystemObject) {
// do something
}}
Have fun.