Full Version: Reference An Object And Control In Sql Statement
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
SteveStew4444
Hi UA!
Hope everyone is well.
I have something that is stumping me.
I have a table that has every Object and Control in my database. I am using it to run some code that will change all the visible labels to Russian.
I am stuck on the Sql statement that will query a Form and a Control by its value. For example, the code will run through a recordset of the table and get the Form and Control name, then it is supposed to create another recordset to find the matching Form name and Control name.

Here's the code and where it errors:

Public Function ImportRussian()
On Error GoTo Err_Handler

Dim rst As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim obj As AccessObject
Dim frm As Form
Dim ctl As Control
Dim strObjectName As String
Dim strParentObject As String
Dim strSQL As String
Dim strSQL2 As String

strSQL = "SELECT * FROM tblLabelList ORDER BY LabelID ASC"
Set rst = CurrentDb.OpenRecordset(strSQL)

' Loop through all the forms and open them in design view.
For Each obj In CurrentProject.AllForms
strParentObject = obj.Name

DoCmd.OpenForm strParentObject, acDesign
Set frm = Forms(strParentObject)

' Loop through all the controls on the form.
For Each ctl In frm.Controls
strObjectName = ctl.Name

'Here's where I can not get the Syntax right....
strSQL2 = "SELECT * FROM tblLabelList WHERE [ParentObject]= " & obj.Name & " And [ObjectName]= " & ctl.Name
'OR below, errors with "...too few parameters"
'strSQL2 = "SELECT * FROM tblLabelList WHERE tblLabelList.ParentObject = strParentObject AND tblLabelList.ObjectName = strObjectName"
Set rs2 = CurrentDb.OpenRecordset(strSQL2)

If Not rs2.EOF Then
rs2.Edit
ctl.Caption = rs2!ObjectCaption
rs2.Update
rs2.Close
End If
rst.MoveNext

Next ctl
DoCmd.Close acReport, strParentObject
Next obj

MsgBox "Complete"

Exit_Proc:
On Error Resume Next
Set frm = Nothing
Set rpt = Nothing
rst.Close
Exit Function

Err_Handler:
MsgBox Err.Number & " " & Err.Description, vbCritical, "FillLabelList()"
Resume Exit_Proc
Resume
End Function

Any help or direction would be appreciated!
Thanks,
Steve
theDBguy
Hi Steve,

QUOTE (SteveStew4444 @ May 23 2012, 06:47 PM) *
'Here's where I can not get the Syntax right....
strSQL2 = "SELECT * FROM tblLabelList WHERE [ParentObject]= " & obj.Name & " And [ObjectName]= " & ctl.Name

Try enclosing the variables in single quotes. For example:

CODE
strSQL2 = "SELECT * FROM tblLabelList WHERE [ParentObject]= '" & obj.Name & "' And [ObjectName]= '" & ctl.Name & "'"


QUOTE
'OR below, errors with "...too few parameters"
'strSQL2 = "SELECT * FROM tblLabelList WHERE tblLabelList.ParentObject = strParentObject AND tblLabelList.ObjectName = strObjectName"

Try moving the variables outside of the string. For example:

CODE
strSQL2 = "SELECT * FROM tblLabelList WHERE tblLabelList.ParentObject = '" & strParentObject & "' AND tblLabelList.ObjectName = '" & strObjectName & "'"


Just my 2 cents... 2cents.gif
SteveStew4444
Hi the DBguy,

Works like a charm!
Thank you so much! What is the purpose of the single quotes. I've seen that before, but don't understand it.

Thank you,
Steve
theDBguy
Hi Steve,

yw.gif

The single quotes are required delimiters for text or string values. You can also use double quotes, but it gets trickier.

Good luck with your project.
SteveStew4444
I see!
Thanks again!
Steve
theDBguy
No problem. thumbup.gif

Cheers cheers.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.