Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

Create connection string from Access to SQL Server sitting on a VMWare virtual machine

I have SQL Server 2014 sitting on a VM on my desktop.  That particular VM also has Office 2003 installed and I do not want to complicate manners by installing Access 2013 on that VM.

So what I need to do is create a linked table on my primary computer which is linked to the SQL Server on the VM, with a DSN-less connection.

Any help would be greatly appreciated.
0
Dale Fye
Asked:
Dale Fye
  • 7
  • 4
  • 4
  • +2
1 Solution
 
Dale FyeAuthor Commented:
Oh, BTW, both the primary computer are running Windows 7 Pro
0
 
ste5anSenior DeveloperCommented:
The architecture using VMs does not matter. You need first to ensure that you can reach that VM. So can you ping it?

Then you just need to connect it:

Public Function TableLinkSqlAsTable(ASourceDatabase As String, ASourceSchema As String, ASourceName As String, ADestinationName As String, APrimaryKeyFields As String) As Boolean
    
  Const CONNECTION_STRING As String = "ODBC;Driver={SQL Server Native Client 1{0}.0};Server={1};Database={2};Trusted_Connection=yes;"
  
  Const SQL_CREATE_INDEX As String = "CREATE INDEX PK_{0} ON {0} ( {1} ) WITH PRIMARY;"
  Const SQL_DROP_INDEX As String = "DROP INDEX {0} ON {1};"
  
  On Local Error GoTo LocalError
     
  Dim Index As DAO.Index
  Dim TableDef As DAO.TableDef
 
  Dim Count As Integer
  
  TableLinkSqlAsTable = False
  ASourceName = ASourceName
  If ADestinationName = "" Then
    ADestinationName = ASourceName
  Else
    ADestinationName = ADestinationName
  End If
         
  QueryDelete ADestinationName
  TableDelete ADestinationName
  Set TableDef = CurrentDbC.CreateTableDef(ADestinationName)
  TableDef.Connect = FormatStr(CONNECTION_STRING, "0", GetServerIP, ASourceDatabase)
  TableDef.SourceTableName = FormatStr("{0}.{1}", ASourceSchema, ASourceName)
  
  On Local Error Resume Next
  
  CurrentDbC.TableDefs.Append TableDef
  If Err.Number <> 0 Then
    On Local Error GoTo LocalError
    TableDef.Connect = FormatStr(CONNECTION_STRING, "1", GetServerIP, ASourceDatabase)
    CurrentDbC.TableDefs.Append TableDef
  End If
  
  On Local Error GoTo LocalError
   
  If APrimaryKeyFields <> "" Then
    For Each Index In TableDef.Indexes
      If Index.Primary Then
        SqlExecuteFmt SQL_DROP_INDEX, Index.Name, ADestinationName
        Exit For
      End If
    Next Index
     
    SqlExecuteFmt SQL_CREATE_INDEX, ADestinationName, APrimaryKeyFields
  End If
    
  CurrentDbC.TableDefs.Refresh
  Set Index = Nothing
  Set TableDef = Nothing
  TableLinkSqlAsTable = True
  Exit Function
  
LocalError:
  DebugError "TableLinkSqlAsTable", ASourceName
  
End Function

Open in new window


with

Public Function FormatStr(AString As String, ParamArray AValues() As Variant) As String

  Dim Count As Long
  Dim Result As String
  
  Result = AString
  
  For Count = 0 To UBound(AValues())
    Result = Replace(Result, "{" & Count & "}", Nz(AValues(Count), "NULL"))
  Next Count
  
  Result = Replace(Result, "{CRLF}", vbCrLf)
  FormatStr = Result

End Function

Public Function SqlExecuteFmt(AStatement As String, ParamArray AValues() As Variant) As Boolean

  Dim Count As Long
  Dim Result As String
  Dim Statement As String

  Statement = AStatement
  For Count = 0 To UBound(AValues())
    Statement = Replace(Statement, "{" & Count & "}", Nz(AValues(Count), "NULL"))
  Next Count

  SqlExecuteFmt = SqlExecute(Statement)

End Function

Public Function SqlExecute(AStatement As String, Optional ASilent As Boolean, Optional ARecordsAffected As Long) As Boolean

  On Local Error GoTo LocalError

  Dim OldMousePointer  As Long

  OldMousePointer = Screen.MousePointer
  Screen.MousePointer = ccHourglass
  SqlExecute = False
  CurrentDbC.Execute AStatement, dbFailOnError Or dbSeeChanges
  ARecordsAffected = CurrentDbC.RecordsAffected
  SqlExecute = True
  Screen.MousePointer = OldMousePointer
  Exit Function

LocalError:
  Screen.MousePointer = OldMousePointer
  Debug.Print ""&H" & Hex(Err.Number); Err.Description; "SQL: " & AStatement
  If Not ASilent Then
     MsgBox(Err.Description)        
  End If

End Function

Open in new window


And I guess you can figure out the outer helper methods..
0
 
Dale FyeAuthor Commented:
@ste5an,

I guess that is my issue, I'm not sure how to ping that VM.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
QlemoC++ DeveloperCommented:
Just try?! Of course you need to be able to access the VM prior to doing anything else.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Dale,

 Go into the VM.

 Drop to a command prompt and type:

ipconfig /all

 You'll see a IP Address listed under the VM NIC.   Use that.   To verify, type:

 Ping <ip address>

 You should get replies.

 Then type SET with a return.   Look for "HOSTNAME"

Then try:

 Ping <host name>

You should get reply's back.  If not, DNS is messed up, but you will be able to use the IP.   If that works, go to the host machine, open a command prompt and do the same thing.

 If it's working there, your set, just use that host name in the connection string.

Jim.
0
 
ste5anSenior DeveloperCommented:
hmm, it's some time, that I've used VMware..  but the when using VMware Workstation then the necessary virtual switch is created automatically.

So you only need to check, whether you VM guest has a static IP ( ipconfig /all ) or an resolvable name (depending on your home router and DHCP).
0
 
Dale FyeAuthor Commented:
@Jim

Got the IP and the Ping worked, but no "HostName" listed after issuing the SET command.

But I'll try it with the IP address.
0
 
Dale FyeAuthor Commented:
Pinging the IP address from the host did not work.  "Request timed out"
0
 
QlemoC++ DeveloperCommented:
The Windows Firewall service of the VM might block inbound traffic. In Start Menu, type "Advanced Firewall", then call the applet displayed, and select "Properties" from the context menu inside of the applet. Then make sure the firewall is either off or not blocking inbound traffic (default is to block everything not explicitely allowed by rules).
0
 
Vadim RappCommented:
1.  Link the table by using regular way, i.e. by using "link table" wizard via ODBC, and create new DSN entry. The process is described in this article:

https://support.office.com/en-us/article/Import-or-link-to-SQL-Server-data-a5a3b4eb-57b9-45a0-b732-77bc6089b84e

under "Link to sql server data". The name of sql server machine is the network name of your V.M.

2. Once you have linked the table, verify that you can see the actual data.

3. In fact, this link is already DSN-less (you can now remove DSN entry you created at step 1, the linked table will still work), but you can adjust it if you want. For that, open the linked table in design view, then right-click, select "Properties", and adjust the value in "Description". If this Access database is supposed to be used on other computers, it makes sense to remove WSID, and maybe adjust APP to the specific project, for better recognition. No coding is required.

In order to step 1 to succeed, you need to

1. ensure that your v.m. is configured with network adapter and there's network connection between your host and v.m - here's a video:
https://www.youtube.com/watch?v=-1_lSAmLerM

2. disable firewall on virtual machine

3. ensure that sql server on v.m. allows remote access:
https://technet.microsoft.com/en-us/library/ms156468(v=sql.105).aspx
https://blogs.msdn.microsoft.com/sql_protocols/2008/04/30/steps-to-troubleshoot-sql-connectivity-issues/
0
 
Vadim RappCommented:
> but no "HostName" listed after issuing the SET command.

Look for COMPUTERNAME.
0
 
Dale FyeAuthor Commented:
@Vadim,

Found "ComputerName", interesting that it changed the "_" in the original to a "-", but still returned an error on the Ping.  My guess is that it has to do with the network adapter and/or firewall mentioned in your previous post.  Will have to check that later today.

Dale
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
You can as a work around put an entry in your HOSTs file for the VM.

It's in:

C:\Windows\System32\drivers\etc

It's a text file.  Edit it and on the last line type the computer name followed by the IP address.

Save it, then test with a Ping by computername.

Past that, your down to firewall and/or SQL Server settings.

Jim.
0
 
QlemoC++ DeveloperCommented:
Underscores are not "allowed" in DNS names officially, so they are changed to dashes if used in a NetBIOS name. A NetBIOS name wasn't allowed to contain a dash in the (very) past, so some translation back and forth took place, and may because of compatibility settings.
0
 
Dale FyeAuthor Commented:
still working on this guys, don't close the question
0
 
Vadim RappCommented:
With due respect, I object to awarding the points to the answers that suggested writing code, because the code is totally unnecessary. The question was how to create dsn-less connection from Access to remote sql server. Assigning points creates an impression that this can only be achieved programmatically, which is not so. The very short answer is "create them as usual", in the 100% regular way described in Access Help, and in Microsoft article I cited in my comment.
0
 
QlemoC++ DeveloperCommented:
Vadim,

whether code is used or the manual setup you describe is not your decision. Without considering the OP background (as far as we can see from this thread), and with no response by the OP, both answers are equally useful. Looking at the level of communication your way is slightly more appliable, IMHO.

My suggestion is to just accept exactly those two comments, https:#a41728935 (Assist) and https:#a41729855 (Best).
0
 
Vadim RappCommented:
My point is exactly that writing code for something that can be achieved by standard, simple, and documented means, is not reasonable - unless there was specific request to write the code, such as to make the operation of linking the table reusable by a single button click, or to make it fully automatic within an application. Compare to a question "how to make the text bold in Word?", where the suggestion would be to write the code, rather than to press ctrl-B or to click the button in toolbar.
0
 
Dale FyeAuthor Commented:
Sorry, guys, this fell off my radar.  Hope this resolves the issue.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 7
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now