DLookup как ControlSource через код VBA - PullRequest
0 голосов
/ 30 ноября 2011

Ниже в моем источнике управления и работает:

=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>"",DLookUp("[CustomerSKU]","ItemCustomerSKU","ItemNumber = '" & Forms!GARF!GARFsubForm![finished_good_#] & "' AND Customer = '" & Forms!GARF!customer & "' AND Company = '" & Forms!GARF!company & "' AND MarketingSeason = '" & DLookUp("[Code]","IDtoCode","ID = " & Forms!GARF!season_id) & "' AND Season = " & Forms!GARF!GARFyear),"")

Я хотел бы назначить его другому источнику управления через код VBA, например:

Forms![GARF]![GARFsubForm]![SKU].ControlSource = (the above code)

Forms![GARF]![GARFsubForm]![SKU]столбец в таблице данных.

ОБНОВЛЕНИЕ: С помощью Ремоу мне удалось найти следующее рабочее решение: код в событии Form_Load:

Forms![GARF]![GARFsubForm]![SKU].ControlSource = 
    "=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>''," 
    & "DLookUp(""[CustomerSKU]"",""ItemCustomerSKU"",""ItemNumber = '"" 
    & Forms!GARF!GARFsubForm![finished_good_#] & ""' AND Customer = '" _
    & Forms!GARF!customer & "' AND Company = '" _
    & Forms!GARF!company & "' AND Season = " & Forms!GARF!GARFyear _
    & " AND MarketingSeason = '" 
    & DLookup("[Code]", "IDtoCode", "ID = " & Forms!GARF!season_id) & "'"")" & ",'')"

1 Ответ

1 голос
/ 30 ноября 2011

Есть несколько проблем.Вот что возвращает ваша строка (для ясности разбитая на строки):

=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>"",
DLookUp([CustomerSKU],ItemCustomerSKU,"ItemNumber = '" 
& Forms!GARF!GARFsubForm![finished_good_#] 
& " ' AND Customer = '" & Forms!GARF!customer 
& "' AND Company = '" & Forms!GARF!company 
& "' AND MarketingSeason = '" & DLookUp([Code],IDtoCode,"ID = " 
& Forms!GARF!season_id) & "' AND Season = " & Forms!GARF!GARFyear),"")

Как видите, эти два не указаны в кавычках и должны быть:

DLookUp([CustomerSKU],ItemCustomerSKU,

Здесь есть дополнительный пробел:

& " ' AND Customer = '" & Forms!GARF!customer 

Является ли текст finished_good_# похожим season_id?

Часто можно использовать одинарную кавычку «вместо», особенно если вы избегаете любого вхождения «в тексте с двумя», например, = O''Reilly '

EDIT

Вы делаете это в коде, поэтому должна быть возможность сделать жизнь проще, например:

A = DLookup("[Code]", "IDtoCode", "ID = " & Forms!GARF!season_id)
B = "DLookUp(""[CustomerSKU]"",""ItemCustomerSKU"",""ItemNumber = '" _
    & Forms!GARF!GARFsubForm![finished_good_#] & "' AND Customer = '" _
    & Forms!GARF!customer & "' AND Company = '" _
    & Forms!GARF!company & "' AND Season = " & Forms!GARF!GARFyear _
    & " AND MarketingSeason = '" & A & "'"")"


s = "=IIf(Forms!GARF!GARFsubForm![finished_good_#]<>''," & c & ",'')"

Debug.Print s
...