472,351 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

How Open Database to search for a table using vba to display its data properties.

Hi Everyone,
I am working on a piece of code, that displays the properties(Name, Datatype, size & Description) of the table in the database.
Now I want to further Enhance the code.
I Have created a form in MS Access, on that form, I have 2 buttons & a text box.
One button is to select a mdb file, whom properties i want to display. Second button then stores that properties in a Excel file.
when I open the file using first button, the path of the file gets stored in the TextBox. Now what i want is that second button that generates the properties to take that path & open that database. But I couldn't able to link that path in the text box to open that database connection.

The code for both buttons is like this:
*********************************
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Command0_Click()
  4. Dim mesPath, mestab, messec As String
  5.  
  6. messec = ""
  7. Call CreateSchemaFile("\\ohsstor2\home\sluthra\My Documents\schemas\")
  8. End Sub
  9.  
  10. Private Sub Command6_Click()
  11. Dim AccessPath As String
  12. AccessPath = BrowseForfile("C:\", "MS Access Files (.mdb)|*.mdb")
  13. Text4.Value = AccessPath
  14.     If AccessPath = "" Then: MsgBox "No path provided - stopping": Exit Sub
  15. End Sub
  16.  
  17. Private Function CreateSchemaFile(sPath As String)
  18.  
  19. Dim Msg As String ' For error handling.
  20. On Error GoTo Err_ShowDescrip
  21.    Dim ws As Workspace, db As DAO.Database
  22.    Dim tblDef As DAO.TableDef, fldDef As DAO.Field
  23.    Dim i As Integer, Handle As Integer
  24.    Dim fldName As String, fldDataInfo As String
  25.    Dim dbpath As String
  26.  
  27.  
  28.  
  29.  
  30.    ' -----------------------------------------------
  31.    ' Set DAO objects.
  32.    ' -----------------------------------------------
  33.     dbpath = "Text4.value"
  34.     szConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & "; "
  35.     SQL1 = "Select * from NADB;"
  36.     Set rst = CreateObject("ADODB.Recordset")
  37.     rst.Open SQL1, szConnStr, 1, 3
  38.     'Set db = CurrentDb()
  39.    ' -----------------------------------------------
  40.    ' Open schema file for append.
  41.    ' -----------------------------------------------
  42.  
  43.     sPath = "\\ohsstor2\home\sluthra\My Documents\schemas\"
  44.     sTblQryName = "NADB"
  45.     sSectionName = sTblQryName
  46.     bIncFldNames = True
  47.  
  48.    fname = "schema_" & sTblQryName & ".ini"
  49.  
  50.    Handle = FreeFile
  51.    Open sPath & "schema_" & sTblQryName & ".xls" For Output Access Write As #Handle
  52.    ' -----------------------------------------------
  53.    ' Write schema header.
  54.    ' -----------------------------------------------
  55.    Print #Handle, "Table : "; sSectionName
  56.    'Print #Handle, "ColNameHeader = " & IIf(bIncFldNames, "True", "False")
  57.    Print #Handle, "Created BY : SUNNY LUTHRA"
  58.    Print #Handle, "Date : "; Now
  59.    Print #Handle, "" & "Column Name" & Chr(9) & "Data Type" & Chr(9) & "Size" & Chr(9) & "Description"
  60.    ' -----------------------------------------------
  61.    ' Get data concerning schema file.
  62.    ' -----------------------------------------------
  63.       Set tblDef = db.TableDefs(sTblQryName)
  64.    With tblDef
  65.       For i = 0 To .Fields.Count - 1
  66.          Set fldDef = .Fields(i)
  67.          With fldDef
  68.             fldName = .Name
  69.             Select Case .Type
  70.                Case dbBoolean
  71.                   fldDataInfo = "Bit" & Chr(9)
  72.                Case dbByte
  73.                   fldDataInfo = "Byte" & Chr(9)
  74.                Case dbInteger
  75.                   fldDataInfo = "Short" & Chr(9)
  76.                Case dbLong
  77.                   fldDataInfo = "Integer" & Chr(9)
  78.                Case dbCurrency
  79.                   fldDataInfo = "Currency" & Chr(9)
  80.                Case dbSingle
  81.                   fldDataInfo = "Single" & Chr(9)
  82.                Case dbDouble
  83.                   fldDataInfo = "Double" & Chr(9)
  84.                Case dbDate
  85.                   fldDataInfo = "Date" & Chr(9)
  86.                Case dbText
  87.                   fldDataInfo = "Char " & Chr(9) & Format$(.Size)
  88.                Case dbLongBinary
  89.                   fldDataInfo = "OLE" & Chr(9)
  90.                Case dbMemo
  91.                   fldDataInfo = "LongChar" & Chr(9)
  92.                Case dbGUID
  93.                   fldDataInfo = "Char" & Chr(9) & "16"
  94.             End Select
  95.            flddescrip = .Properties("Description")
  96.  
  97.             Print #Handle, "" & fldName & _
  98.                              Chr(9) & fldDataInfo & Chr(9) & flddescrip
  99.          End With
  100.                Next i
  101.    End With
  102.    MsgBox sPath & fname & " has been created."
  103.    CreateSchemaFile = True
  104. CreateSchemaFile_End:
  105.    Close Handle
  106. Exit_ShowDescrip:
  107. Exit Function
  108. Err_ShowDescrip:
  109. If Err = 3270 Then
  110. flddescrip = ""
  111. Resume Next
  112. Else
  113. MsgBox (Err & ": " & Error$), , "ShowDescrip()"
  114. End If
  115. End Function
  116.  
  117.  
  118.  
  119. Private Function BrowseForfile(pstrPath, pstrFilter)
  120. Set objDialog = CreateObject("UserAccounts.CommonDialog")
  121. objDialog.Filter = pstrFilter
  122. objDialog.InitialDir = pstrPath
  123. objDialog.Flags = &H80000 + &H4 + &H8
  124. intResult = objDialog.ShowOpen
  125. BrowseForfile = objDialog.FileName
  126.  
  127.             If intResult = 0 Then
  128.             MsgBox "No file selected - Exiting"
  129.             End If
  130. End Function
  131.  
**********************************
Can Anyone Knows How to set the database path here:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & "; "
So that instead of defining the path of data source.
Data Source should automatically takes the value in the text box or the file I open using file dialog box.
Or Anyone Having working code for opening an mdb file & displaying the properties of the tables. & then stores it in Excel file.
Jan 8 '08 #1
1 3592
JKing
1,206 Expert 1GB
Hi there,

Could you not just alter your CreateSchemaFile Function to take both an spath and a dpath. This way when you call the function you can pass in the returned value of BrowseForFile as the dpath.

Jking
Jan 8 '08 #2

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

Similar topics

4
by: Rob Meade | last post by:
Hi all, Ok - this leads on from speaking to a couple here and in the SQL server group... I've an application which allows the user to type in...
3
by: Daniel M | last post by:
I'm building a medium-scale data-entry web application, which involves creating data entry forms, record listings and detail screens for lots of...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10...
5
by: plenahan68 | last post by:
I've made a small vb.net program to read my database and delete records that are specific to my needs. Well, the program says it's complete but...
4
by: visionstate | last post by:
Hi there, I'm fairly new to access and the way it works and I have been building a relatively simple database. It is basically a database where...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since...
3
by: toffee | last post by:
Hi all, I got a pre-formatted spreadsheet. would it be possible using js to copy the data from a table on the current webpage, open the...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project...
12
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.