Share via

Access 2013 - control.RowSource = Execute SQL statement result Little Help

Anonymous
2014-10-30T16:50:23+00:00

Hi

I like to bound data to a control straight from SQL server

dim ServerConnection As String = "Provider=SQLOLEDB; Data Source=Development; Initial Catalog=Production_LV; Integrated Security=SSPI;"

Dim rs As ADODB.Recordset

Dim conn As ADODB.Connection

Set conn = New ADODB.Connection

conn.Open ServerConnection      

Set rs = New ADODB.Recordset

Dim sql As String

sql = "SELECT Emp_Name, IDInt, Emp_Code, Mgt_Function FROM Emp_Mst WHERE([Emp_Log]='Cleaning' AND [Active]=1) ORDER BY Emp_Name"

 Set rs = conn.Execute(sql)

control.RowSource = rs.source       - Throw Error The name of the recordsource exist in a different database

This is does work when I had linked table to access ,I have requirements to  use it without linked table.

Can it be done? or Do I have to loop thru records and add each one ?

Thanks

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-10-30T22:41:37+00:00

If I understand you properly

Create query in access and use it- (that would probably  work) but,

My requirements are unbound Forms and any Data operation or Query does SQL Server.

No, that isn't what I meant.  What I am suggesting is:

  1. Create a pass-through query in Access that works.
  2. Open that query in design view.  Open its property sheet, copy the value of the "ODBC Connect Str" property (which is the .Connect property of the querydef) to the clipboard, then paste that between  the  brackets in the SQL string in your code.

3,  Delete the pass-through query.

I figure that Access will make a connect string for the query that it knows how to resolve, and that you should be able to use that same connect string.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2014-10-30T20:59:16+00:00

You may be able to do something like this:

sql = "SELECT Emp_Name, IDInt, Emp_Code, Mgt_Function " & _

"FROM [ODBC;Provider=SQLOLEDB;Data Source=Development;Initial Catalog=Production_LV;Integrated Security=SSPI].Emp_Mst " & _

"WHERE([Emp_Log]='Cleaning' AND [Active]=1) ORDER BY Emp_Name"

control.RowSource = sql

That is, use the connection string, enclosed in square brackets, as a qualifier for the table name in a SQL string that you assign to the control's RowSource property.  I'm not completely sure this will work, but similar syntax works in other cases.

The connection string I suggested in my example may not be correct.  I suggest you create a pass-through query to this SQL Server database, make sure it works, and then open that query in design view and look at its Connect property.  That ought to give you a value that Access will accept.

Was this answer helpful?

0 comments No comments

15 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-10-30T18:48:08+00:00

    It will be control.RowSource = tableName or a Sql based on tableName

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-10-30T17:11:21+00:00

    No I cannot (How would it help if I could?)

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-10-30T17:04:25+00:00

    Hi,

    can you change your query to a creation table query?

    Mimmo

    Was this answer helpful?

    0 comments No comments