|
-
May 30th, 2008, 03:24 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] VBA - Copy database from one destination to another
Hello everyone,
I am working with MS Access databases (.mdb) Can anyone please give me a simple directive on copying a database from one destination to another (from Source to destination)
Actually I am intend to copy the database from a Server db to my local C:\ drive.
Below is my set up so far:
Code:
Private Sub cmddbCopy_Click()
Dim dbsource As Database
Dim dbdestination As Database
Set dbdestination = CurrentDb
Set dbsource = OpenDatabase("M:\\Livctrls03-08\data1\App\UCCProd\Databases\Unclaimed Checks.mdb", True, True)
Set dbdestination = OpenDatabase("C:Unclaimed Checks.mdb", True, False)
Any hint will be greatly appreciated.
Giftx.
-
May 30th, 2008, 03:28 PM
#2
Re: VBA - Copy database from one destination to another
If this is a simple task to copy the entire database over, then because it's an MDB, use the FileCopy method to copy the MDB over.
-
May 30th, 2008, 03:34 PM
#3
Thread Starter
Hyperactive Member
Re: VBA - Copy database from one destination to another
Thanks for your quick response.
Question: should I create the destination database already then copy over the data or will it just copy the entire database and name it?
Also, can you give me a code example
Thanks.
GiftX.
-
May 30th, 2008, 03:36 PM
#4
Re: VBA - Copy database from one destination to another
MS Access databases are simply .MDB files. Treat it like any other file. This means that you needn't create the database first, just overwrite it if the file already exists.
-
May 30th, 2008, 03:38 PM
#5
Re: VBA - Copy database from one destination to another
To be safe you will want to make sure no users are in hte db too.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 30th, 2008, 03:39 PM
#6
Thread Starter
Hyperactive Member
Re: VBA - Copy database from one destination to another
Please, can you give me an example of what you mean? Also, you can see an excerpt of the code I started in my thread.
Giftx.
-
May 30th, 2008, 03:46 PM
#7
Re: VBA - Copy database from one destination to another
Are you doing this from within Access' VBA IDE?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 30th, 2008, 03:54 PM
#8
Thread Starter
Hyperactive Member
Re: VBA - Copy database from one destination to another
Hi Rob.
Yes, I am doing this from MS Access environment.
-
May 30th, 2008, 04:27 PM
#9
Re: VBA - Copy database from one destination to another
Hi Giftx
If you are copying the mdb as any other file then check these three ways... take your pick 
1) API WAY
you can use this API. this works in vb6. I am sure this will work in vba as well.
Code:
Option Explicit
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private Const FO_COPY = &H2
Public Function ShellFileCopy(src As String, dest As String, _
Optional NoConfirm As Boolean = False) As Boolean
'PURPOSE: COPY FILES VIA SHELL API
'THIS DISPLAYS THE COPY PROGRESS DIALOG BOX
'PARAMETERS: src: Source File (FullPath)
'dest: Destination File (FullPath)
'NoConfirm (Optional): If set to
'true, no confirmation box
'is displayed when overwriting
'existing files, and no
'copy progress dialog box is
'displayed
'Returns (True if Successful, false otherwise)
'EXAMPLE:
'dim bSuccess as boolean
'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt")
Dim WinType_SFO As SHFILEOPSTRUCT
Dim lRet As Long
Dim lflags As Long
lflags = FOF_ALLOWUNDO
If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION
With WinType_SFO
.wFunc = FO_COPY
.pFrom = src
.pTo = dest
.fFlags = lflags
End With
lRet = SHFileOperation(WinType_SFO)
ShellFileCopy = (lRet = 0)
End Function
2) FSO WAY
Code:
Dim fs As Object
Dim oldPath As String, newPath As String
oldPath = "C:\Documents and Settings\user\My Documents" 'Folder file is located in
newPath = "C:\Documents and Settings\user\My Documents\Misc" 'Folder to copy file to
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath & "\" & "myfile.mdb", newPath & "\" & "myfile.mdb"
Set fs = Nothing
3) FILE COPY
Code:
Dim sDest As String
Dim sSource As String
sSource = "\\MYSERVER\KOOLSID\MYFILE.MDB"
sDest = "C:\BACKUP\MYFILE.MDB"
FileCopy sSource, sDest
Hope this helps...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
May 30th, 2008, 04:35 PM
#10
Re: VBA - Copy database from one destination to another
Ah I see you like the API way too I have code similar to that on the Forums but for folder copying.
Copying from within Access while its open will work but you will need to either read the ldb file to see if anyone else is in it or try other techniques. You will be limited since you are in Access already.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jun 6th, 2008, 12:14 PM
#11
Thread Starter
Hyperactive Member
Re: VBA - Copy database from one destination to another
Thanks all you Gurus. All the examples will do just fine.
Thanks and enjoy your weekend.
Giftx.
-
May 23rd, 2011, 06:35 AM
#12
New Member
Re: [RESOLVED] VBA - Copy database from one destination to another
Hey All,
I have a question related to this.
I am using API way to copy data, but my user is on a VPN and VPN gets disconnected in between and excel throws out an error. Is there a way to copy data bit by bit from one location to another?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|