Creating MS Access DB from C/AL
sanjeev_baranwal
Member Posts: 101
Can someone help me for how to create a MS access DB and a table from Navision through code. Waiting for reply :?:
0
Comments
-
Hm... maybe copying an existing empty MDB through a SHELL command?
For table, ADO? http://mibuso.com/dlinfo.asp?FileID=5890 -
I would start by trying the Access object model. Search for a VBA code snippet and try to replicate it in Navision with the use of automation variables. Check out the Excel buffer table to see how it is done with Excel automation, you can probably do something similar with the Access object model.0
-
Hi thanks for the reply.
By the way Miklos It is a good trick to copy the mdb file but my question is how to create through code. and Daniel I think I must use ADOX in automation and then create tables in it through it. I am trying to do it and how it end wil let u know and will also loook for Access Object Model. I will try first to complete in vb and then write the corresponding in Navision.0 -
Hi,
I just looked around on library of mdsn.com, and found following code:
' Include following in Declarations section of module.
Dim appAccess As Access.Application
Sub NewAccessDatabase()
Dim dbs As Object, tdf As Object, fld As Variant
Dim strDB As String
Const DB_Text As Long = 10
Const FldLen As Integer = 40
' Initialize string to database path.
strDB = "C:\My Documents\Newdb.mdb"
' Create new instance of Microsoft Access.
Set appAccess = _
CreateObject("Access.Application.9")
' Open database in Microsoft Access window.
appAccess.NewCurrentDatabase strDB
' Get Database object variable.
Set dbs = appAccess.CurrentDb
' Create new table.
Set tdf = dbs.CreateTableDef("Contacts")
' Create field in new table.
Set fld = tdf. _
CreateField("CompanyName", DB_Text, FldLen)
' Append Field and TableDef objects.
tdf.Fields.Append fld
dbs.TableDefs.Append tdf
Set appAccess = Nothing
End Sub
This example above makes use of access object. I would not myself create tables like that. I really prefer ADO and DDL way. By using DDL (data definition language, is comparable to sql but then specific commands to create tables, fields, keys, relations and so on) you have all the possibilties like mentioned below
dim strsql as string
dim cn as adodb.connection
cn.connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=databaseName;User ID=MyUserID;Password=MyPassword;"
User ID and Password must not be used i thought
Data Source= c:\mydatabase.mdb
provider = like mentioned above to connect to a ms access database
strsql=CREATE TABLE tblCustomers (
CustomerID INTEGER NOT NULL,
[Last Name] TEXT(5) NOT NULL,
[First Name] TEXT(50) NOT NULL,
Phone TEXT(10),
Email TEXT(50))
cn.Execute (strsql)
table tblCustomers is now created =D>
This command uses ado (Be carefully, adox is a smaller footprint version of ado, i allways use ado. I just thought that using DDL was one of the points that was not implemented all the way in adox, but i don't know exactly by heart for the moment)
This should help you to start with.
Francois0 -
Hi Francois,
u have given a reply which can solve my problems I am sure.0 -
Based on the code provided by Francois I am trying to replicate it in C/AL but I am finding it a bit tricky.
This is what I have so far:
DB_Text := 'Sony';
FldLen := 40;
g_txtNewDatabase := 'C:\Newdb.mdb';
CREATE(AccessApp);
AccessApp.NewCurrentDatabase(g_txtNewDatabase);
dbs := AccessApp.CurrentDb;
tdf := dbs.CreateTableDef("Contacts");
fld := tdf.CreateField("CompanyName", DB_Text, FldLen);
tdf.Fields.Append(tdf);
It seems to create the database okay, but I am having trouble creating the tables and fields.
What data types should bds, tdf and fld be?
Your advice would be truly appreciated!Robbiex0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 322 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions
