I am very new to SQL and think I have a simple problem but was unable to figure it out from other posts. I have the following code:
INSERT INTO tblShortScores ( TradeNum, FilterNum, Rank, ScoreNum ) SELECT [Forms]![frmOpenTrades]![TradeNum] AS TradeNum, tblFilters.FilterNum, tblFilters.SBBExh AS Rank, tblFilters.SBBExh AS Score FROM tblFilters WHERE (((tblFilters.SBBExh) Is Not Null));
but instead of using the literal “SBBExh” in tblFilters.SBBExh, I want to do something like
tblFilters.(“S” & [Forms]![frmOpenTrades]![Strategy])
where something like
[Forms]![frmOpenTrades]![Strategy] contains the value “BBExh”.
It’s in MS Access and I seem unable to find a syntax that works
any help is appreciated
Advertisement
Answer
Can’t dynamically build field name in query object. Use VBA to construct and execute action SQL, like:
strField = "S" & Me.Strategy CurrentDb.Execute "INSERT INTO tblShortScores (TradeNum, FilterNum, ScoreNum) " & _ "SELECT " & Me.TradeNum & " AS TradeNum, FilterNum, " & strField & " " & _ "FROM tblFilters WHERE " & strField & " Is Not Null;"
Assumes TradeNum is number type – if it is text, use apostrophe delimiters:
SELECT '" & Me.TradeNum & "' AS
.
If SQL injection is a concern review, How do I use parameters in VBA in the different contexts in Microsoft Access?