Change a query's SQL code from excel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cht13er

    Change a query's SQL code from excel

    Good day all, I'm a newish VBA coder who was taught from Google Groups
    - so thanks to you all very much!

    I have run into a minor problem, I hope you can give me some help ...
    I'm running office 2003 for this one.

    In excel I have a program running ... and based on the results of an
    optionbutton (called "Form2.Radio_Al lParameters") I want to change the
    SQL that runs a query in access. If the button is True, the SQL should
    read "WHERE (((ChemistryRea dings.UsedDate) =Yes) AND
    ((ChemistryRead ings.UsedLocati on)=Yes) AND ((MOE_Tables1to 6.Used)<>No
    Or (MOE_Tables1to6 .Used) Is Null) AND
    ((MOE_Standard_ Definitions.Use d)<>No Or
    (MOE_Standard_D efinitions.Used ) Is Null));"

    but when the button is False it should read ".....AND
    ((MOE_Tables1to 6.Used=Yes) AND...."

    The reasons that I think that I want to change the SQL and not create
    a new query are: (1) it is a crosstab query that's built off of this
    query that is what is imported into excel and (2) I don't know how to
    create the relationships from excel.

    My excel VBA snippet is like this:

    '--------------------------------code start-------------------------
    'Public Sub cmdGo_Click()

    'Declarations
    Dim DB As DAO.Database
    Dim RS As DAO.Recordset
    Dim FLD As DAO.Field
    Dim qryDef1 As DAO.QueryDef
    Dim strSQL As String

    'Recall filepath and open database
    FileName = Sheets("README" ).Cells(1, 20).Value
    Set DB = DAO.OpenDatabas e(FileName)
    '---------------------------------------------------------------------------------
    'Re-create the query "ChemistryQuery " based on whether you want ALL
    the parameters (including those not in the standards) or not:
    Set qryDef1 = DB.QueryDefs("C hemistryQuery")

    If Form2.Radio_All Parameters = True Then
    strSQL = "SELECT [ChemistryReadin gs].[Parcel] & ' ' &
    [Location] & ' ' & [LaboratoryID] & ' ' & [LaboratoryJobNu mber] & ' '
    & [SampleDate] AS Header, Parameter_Names .[CAS#], MOE_Tables1to6.
    [Parameter ID], ChemistryReadin gs.Parameter,
    MOE_Tables1to6. StandardName, MOE_Standard_De finitions.ID,
    MOE_Tables1to6. Standard, MOE_Tables1to6. Units AS MOE_Tables1to6_ Units,
    MOE_Tables1to6. Notes, ChemistryReadin gs.SampleDate,
    ChemistryReadin gs.Reading, ChemistryReadin gs.Units AS Reading_Units,
    ChemistryReadin gs.Comments, ChemistryReadin gs.UsedDate,
    ChemistryReadin gs.UsedLocation , MOE_Tables1to6. Used,
    MOE_Standard_De finitions.Used FROM MOE_Standard_De finitions RIGHT JOIN
    ((Parameter_Nam es RIGHT JOIN ChemistryReadin gs ON
    Parameter_Names .ParameterName = ChemistryReadin gs.Parameter) LEFT JOIN
    MOE_Tables1to6 ON ChemistryReadin gs.Parameter =
    MOE_Tables1to6. Parameter) ON MOE_Standard_De finitions.FullN ame =
    MOE_Tables1to6. StandardName" & _
    "WHERE (((ChemistryRea dings.UsedDate) =Yes) AND
    ((ChemistryRead ings.UsedLocati on)=Yes) AND ((MOE_Tables1to 6.Used)<>No
    Or (MOE_Tables1to6 .Used) Is Null) AND
    ((MOE_Standard_ Definitions.Use d)<>No Or
    (MOE_Standard_D efinitions.Used ) Is Null));"
    Else
    strSQL = "SELECT [ChemistryReadin gs].[Parcel] & ' ' &
    [Location] & ' ' & [LaboratoryID] & ' ' & [LaboratoryJobNu mber] & ' '
    & [SampleDate] AS Header, Parameter_Names .[CAS#], MOE_Tables1to6.
    [Parameter ID], ChemistryReadin gs.Parameter,
    MOE_Tables1to6. StandardName, MOE_Standard_De finitions.ID,
    MOE_Tables1to6. Standard, MOE_Tables1to6. Units AS MOE_Tables1to6_ Units,
    MOE_Tables1to6. Notes, ChemistryReadin gs.SampleDate,
    ChemistryReadin gs.Reading, ChemistryReadin gs.Units AS Reading_Units,
    ChemistryReadin gs.Comments, ChemistryReadin gs.UsedDate,
    ChemistryReadin gs.UsedLocation , MOE_Tables1to6. Used,
    MOE_Standard_De finitions.Used FROM MOE_Standard_De finitions RIGHT JOIN
    ((Parameter_Nam es RIGHT JOIN ChemistryReadin gs ON
    Parameter_Names .ParameterName = ChemistryReadin gs.Parameter) LEFT JOIN
    MOE_Tables1to6 ON ChemistryReadin gs.Parameter =
    MOE_Tables1to6. Parameter) ON MOE_Standard_De finitions.FullN ame =
    MOE_Tables1to6. StandardName" & _
    "WHERE (((ChemistryRea dings.UsedDate) =Yes) AND
    ((ChemistryRead ings.UsedLocati on)=Yes) AND ((MOE_Tables1to 6.Used)<>No
    Or (MOE_Tables1to6 .Used) Is Null) AND
    ((MOE_Standard_ Definitions.Use d)=Yes);"
    End If

    qryDef1.Execute strSQL
    -------------------end code-----------------


    My problem is that I am unsure of IF I can change the SQL of the query
    at all ..... but also how to do so if I can (the last line is what is
    giving me the error called "runtime error 3421 data type conversion
    error".

    Thank you very much for any help that you can supply, I will monitor
    this thread super closely in case I should have provided any more
    information.

    Chris
Working...