Skip to main content
edited tags
Link
Tony Toews
  • 7.9k
  • 1
  • 24
  • 27
fixed code tags
Source Link
Mark
  • 108.8k
  • 20
  • 180
  • 238
Option Explicit

Public Const DataLocation As String = "C:\Documents and Settings\Alice\Desktop\Database\TestDatabase21.accdb" Sub Market_Update() Call ImportFromAccessTable(DataLocation, "Final_Table", Worksheets(2).Range("A5")) End Sub

Sub ImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)

Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer


Public Const DataLocation As String = "C:\Documents and Settings\Alice\Desktop\Database\TestDatabase21.accdb"

Sub Market_Update()
    Call ImportFromAccessTable(DataLocation, "Final_Table", Worksheets(2).Range("A5"))
End Sub

Sub ImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)

    Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
    
    Set TargetRange = TargetRange.Cells(1, 1)
    ' open the database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
    Set rs = New ADODB.Recordset
    With rs
        ' open the recordset
        ' .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
        
        ' all records
        .Open "SELECT * FROM Final_Table", cn, , , adCmdText
        ' filter records
        
        For intColIndex = 0 To rs.Fields.count - 1 ' the field names
            TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
        Next
        TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

    End With
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

End Sub

Sub Company_Information()

Dim companyName As String

On Error GoTo gotoError companyName = Application.InputBox(Prompt:="Enter Company Name", _ Title:="Company Name", Type:=2)

End Sub

Sub Company_Information()

   Dim companyName As String
   
On Error GoTo gotoError

   companyName = Application.InputBox(Prompt:="Enter Company Name", _
                           Title:="Company Name", Type:=2)                  
        
    Exit Sub 'Don't execute errorhandler at end of routine

gotoError:
    MsgBox "An error has occurred"

End Sub

gotoError: MsgBox "An error has occurred" End Sub

The code for this part can be seen following:

Sub UPDATE()

Dim cnt As ADODB.Connection Dim stSQL As String, stCon As String, DataLocation As String Dim stSQL2 As String

'database path - currently same as this workbook DataLocation = ThisWorkbook.Path & DataLocation stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & DataLocation & ";" 'SQL code for GL Insert to Access stSQL = "INSERT INTO Historical_Stock_Data SELECT * FROM [Portfolio] IN '" _ & ThisWorkbook.FullName & "' 'Excel 8.0;'"

'set connection variable Set cnt = New ADODB.Connection 'open connection to Access db and run the SQL With cnt .Open stCon .CursorLocation = adUseServer .Execute (stSQL) End With 'close connection cnt.Close

'release object from memory Set cnt = Nothing

End Sub

Sub UPDATE()

   Dim cnt As ADODB.Connection
   Dim stSQL As String, stCon As String, DataLocation As String
   Dim stSQL2 As String

   'database path - currently same as this workbook
   DataLocation = ThisWorkbook.Path & DataLocation
   stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=" & DataLocation & ";"
   'SQL code for GL Insert to Access
   stSQL = "INSERT INTO Historical_Stock_Data SELECT * FROM [Portfolio] IN '" _
   & ThisWorkbook.FullName & "' 'Excel 8.0;'"
   
   'set connection variable
   Set cnt = New ADODB.Connection
   'open connection to Access db and run the SQL
   With cnt
        .Open stCon
        .CursorLocation = adUseServer
        .Execute (stSQL)
   End With
   'close connection
   cnt.Close

   'release object from memory
   Set cnt = Nothing

End Sub

I get the following error with this.

Run-time Error '-2147467259 (80004005)'

The Microsoft Jet database engine cannot open the file 'Cocuments and Settings\Alice\Desktop\Database'. It is already opened exclusively by another user or you need permission to view its data.

Run-time Error '-2147467259 (80004005)'

The Microsoft Jet database engine cannot open the file 'Cocuments and Settings\Alice\Desktop\Database'. It is already opened exclusively by another user or you need permission to view its data.
Option Explicit

Public Const DataLocation As String = "C:\Documents and Settings\Alice\Desktop\Database\TestDatabase21.accdb" Sub Market_Update() Call ImportFromAccessTable(DataLocation, "Final_Table", Worksheets(2).Range("A5")) End Sub

Sub ImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)

Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer

Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
    ' open the recordset
    ' .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
    
    ' all records
    .Open "SELECT * FROM Final_Table", cn, , , adCmdText
    ' filter records
    
    For intColIndex = 0 To rs.Fields.count - 1 ' the field names
        TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
    Next
    TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub

Sub Company_Information()

Dim companyName As String

On Error GoTo gotoError companyName = Application.InputBox(Prompt:="Enter Company Name", _ Title:="Company Name", Type:=2)

Exit Sub 'Don't execute errorhandler at end of routine

gotoError: MsgBox "An error has occurred" End Sub

The code for this part can be seen following:

Sub UPDATE()

Dim cnt As ADODB.Connection Dim stSQL As String, stCon As String, DataLocation As String Dim stSQL2 As String

'database path - currently same as this workbook DataLocation = ThisWorkbook.Path & DataLocation stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & DataLocation & ";" 'SQL code for GL Insert to Access stSQL = "INSERT INTO Historical_Stock_Data SELECT * FROM [Portfolio] IN '" _ & ThisWorkbook.FullName & "' 'Excel 8.0;'"

'set connection variable Set cnt = New ADODB.Connection 'open connection to Access db and run the SQL With cnt .Open stCon .CursorLocation = adUseServer .Execute (stSQL) End With 'close connection cnt.Close

'release object from memory Set cnt = Nothing

End Sub

I get the following error with this.

Run-time Error '-2147467259 (80004005)'

The Microsoft Jet database engine cannot open the file 'Cocuments and Settings\Alice\Desktop\Database'. It is already opened exclusively by another user or you need permission to view its data.

Option Explicit

Public Const DataLocation As String = "C:\Documents and Settings\Alice\Desktop\Database\TestDatabase21.accdb"

Sub Market_Update()
    Call ImportFromAccessTable(DataLocation, "Final_Table", Worksheets(2).Range("A5"))
End Sub

Sub ImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)

    Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
    
    Set TargetRange = TargetRange.Cells(1, 1)
    ' open the database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
    Set rs = New ADODB.Recordset
    With rs
        ' open the recordset
        ' .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
        
        ' all records
        .Open "SELECT * FROM Final_Table", cn, , , adCmdText
        ' filter records
        
        For intColIndex = 0 To rs.Fields.count - 1 ' the field names
            TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
        Next
        TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

    End With
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

Sub Company_Information()

   Dim companyName As String
   
On Error GoTo gotoError

   companyName = Application.InputBox(Prompt:="Enter Company Name", _
                           Title:="Company Name", Type:=2)                  
        
    Exit Sub 'Don't execute errorhandler at end of routine

gotoError:
    MsgBox "An error has occurred"

End Sub

The code for this part can be seen following:

Sub UPDATE()

   Dim cnt As ADODB.Connection
   Dim stSQL As String, stCon As String, DataLocation As String
   Dim stSQL2 As String

   'database path - currently same as this workbook
   DataLocation = ThisWorkbook.Path & DataLocation
   stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=" & DataLocation & ";"
   'SQL code for GL Insert to Access
   stSQL = "INSERT INTO Historical_Stock_Data SELECT * FROM [Portfolio] IN '" _
   & ThisWorkbook.FullName & "' 'Excel 8.0;'"
   
   'set connection variable
   Set cnt = New ADODB.Connection
   'open connection to Access db and run the SQL
   With cnt
        .Open stCon
        .CursorLocation = adUseServer
        .Execute (stSQL)
   End With
   'close connection
   cnt.Close

   'release object from memory
   Set cnt = Nothing

End Sub

I get the following error with this.

Run-time Error '-2147467259 (80004005)'

The Microsoft Jet database engine cannot open the file 'Cocuments and Settings\Alice\Desktop\Database'. It is already opened exclusively by another user or you need permission to view its data.
Source Link
md85
  • 79
  • 1
  • 7

Access - Excel Integration

Hey all, have been working on designing a new database for work. They have been using Excel for their daily reports and all the data is stored in there, so I decided to have the back-end of the database in Access and the front-end in Excel, so any analytical work can be easily performed once all the data has been imported into Excel.

Now I'm fairly new to VBA, slowly getting used to using it, have written some code to transfer one of the calculated tables from Access to Excel:

Option Explicit

Public Const DataLocation As String = "C:\Documents and Settings\Alice\Desktop\Database\TestDatabase21.accdb" Sub Market_Update() Call ImportFromAccessTable(DataLocation, "Final_Table", Worksheets(2).Range("A5")) End Sub

Sub ImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)

Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer

Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
    ' open the recordset
    ' .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
    
    ' all records
    .Open "SELECT * FROM Final_Table", cn, , , adCmdText
    ' filter records
    
    For intColIndex = 0 To rs.Fields.count - 1 ' the field names
        TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
    Next
    TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub

Sub Company_Information()

Dim companyName As String

On Error GoTo gotoError companyName = Application.InputBox(Prompt:="Enter Company Name", _ Title:="Company Name", Type:=2)

Exit Sub 'Don't execute errorhandler at end of routine

gotoError: MsgBox "An error has occurred" End Sub

The above code works fine and pulls up the desired calculated table and places it in the right cells in Excel.

I've got two problems that I'm having trouble with; firstly I have some cell-formatting already done for the cells where the data is going to be pasted into in Excel; I want it to apply the formatting to the values as soon as they are pasted in Excel.

Secondly; I have an add-on for Excel which updates some daily Stock Market values; these values need to be transferred into Access at the end of each working day, to keep the database maintained, I tried some code but have been having some problems with it running.

The code for this part can be seen following:

Sub UPDATE()

Dim cnt As ADODB.Connection Dim stSQL As String, stCon As String, DataLocation As String Dim stSQL2 As String

'database path - currently same as this workbook DataLocation = ThisWorkbook.Path & DataLocation stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & DataLocation & ";" 'SQL code for GL Insert to Access stSQL = "INSERT INTO Historical_Stock_Data SELECT * FROM [Portfolio] IN '" _ & ThisWorkbook.FullName & "' 'Excel 8.0;'"

'set connection variable Set cnt = New ADODB.Connection 'open connection to Access db and run the SQL With cnt .Open stCon .CursorLocation = adUseServer .Execute (stSQL) End With 'close connection cnt.Close

'release object from memory Set cnt = Nothing

End Sub

I get the following error with this.

Run-time Error '-2147467259 (80004005)'

The Microsoft Jet database engine cannot open the file 'Cocuments and Settings\Alice\Desktop\Database'. It is already opened exclusively by another user or you need permission to view its data.

I'm fairly new to databases, VBA and Access so any help would be greatly appreciated.

Also I have been told that the above method of having an Excel front-end and Access back-end is not recommended but alot of the analysis they conduct is done through Excel, and the charts feature in Excel is much better than Access in my experience atleast; and that is also one of the requirements for this project.

Thank you advance!