MS Access Database insert values on a column using vba and sql query

Praveen KUMAR

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.

Naveen Arora

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Convert MS Access data query into SQL Server database query

分類Dev

Insert just the date to MS access database using FireDac

分類Dev

Reset identity column in MS access database

分類Dev

Using previous entry to calculate MS Access vba

分類Dev

MS Access - SQL append query behavior is erratic

分類Dev

MS Access 2002/VBA - join a number of lookup tables to main query?

分類Dev

How to insert bind column and unbind column datagridview values in database

分類Dev

Updateable query error message in MS Access SQL query

分類Dev

How to save an Access database using VBA program?

分類Dev

Access VBA insert values into webform without predictable element id

分類Dev

Replace and delete column values in SQL Server database

分類Dev

SQL Insert query with missing column and Invalid column name

分類Dev

how to add new items in listbox using VBA in ms access

分類Dev

MS Access 2010 SQL Select Into Calculated Column Issue

分類Dev

How to get text values of rules in MS Outlook using VBA?

分類Dev

MS Access - Using Form Inputs to Filter on Multiple Criteria in a Query

分類Dev

MS Access with MySQL using DAO Connection Parameter Query Error

分類Dev

MS Access with MySQL using DAO Connection Parameter Query Error

分類Dev

MS Access Insert IntoSelectはvbaでは機能しません

分類Dev

SQL Query find values that have all values of second column

分類Dev

SQL SeErver : INSERT statement, calculate two column values

分類Dev

How to insert the multiple row values of a table into the database using php

分類Dev

SQL MS Access 2013

分類Dev

MS Access Link Table With VBA

分類Dev

drivers to communicate with MS SQL database using php 5.5

分類Dev

Shortest Path in MS Access Database

分類Dev

Getting Access form field values in SQL string in the VBA

分類Dev

MS Access - Query - Replace value

分類Dev

Multiple query data in ms access

Related 関連記事

  1. 1

    Convert MS Access data query into SQL Server database query

  2. 2

    Insert just the date to MS access database using FireDac

  3. 3

    Reset identity column in MS access database

  4. 4

    Using previous entry to calculate MS Access vba

  5. 5

    MS Access - SQL append query behavior is erratic

  6. 6

    MS Access 2002/VBA - join a number of lookup tables to main query?

  7. 7

    How to insert bind column and unbind column datagridview values in database

  8. 8

    Updateable query error message in MS Access SQL query

  9. 9

    How to save an Access database using VBA program?

  10. 10

    Access VBA insert values into webform without predictable element id

  11. 11

    Replace and delete column values in SQL Server database

  12. 12

    SQL Insert query with missing column and Invalid column name

  13. 13

    how to add new items in listbox using VBA in ms access

  14. 14

    MS Access 2010 SQL Select Into Calculated Column Issue

  15. 15

    How to get text values of rules in MS Outlook using VBA?

  16. 16

    MS Access - Using Form Inputs to Filter on Multiple Criteria in a Query

  17. 17

    MS Access with MySQL using DAO Connection Parameter Query Error

  18. 18

    MS Access with MySQL using DAO Connection Parameter Query Error

  19. 19

    MS Access Insert IntoSelectはvbaでは機能しません

  20. 20

    SQL Query find values that have all values of second column

  21. 21

    SQL SeErver : INSERT statement, calculate two column values

  22. 22

    How to insert the multiple row values of a table into the database using php

  23. 23

    SQL MS Access 2013

  24. 24

    MS Access Link Table With VBA

  25. 25

    drivers to communicate with MS SQL database using php 5.5

  26. 26

    Shortest Path in MS Access Database

  27. 27

    Getting Access form field values in SQL string in the VBA

  28. 28

    MS Access - Query - Replace value

  29. 29

    Multiple query data in ms access

ホットタグ

アーカイブ