Insert into based on the results of an IIF statment

青春壹個敷衍的年華 提交于 2019-12-11 17:23:33

问题


I want to INSERT the results of

SELECT G.Results
FROM (SELECT IIf([Forms]![Login]![User_Name]=[C_Customers].[user_name]                                       
             And                                                
                [Forms]![Login]![Password]=[C_Customers].[Password]
                ,"Login Successful"
                ,"Login Failed") AS Results 
      FROM C_Customers)  AS G
      WHERE ((G.Results)="Login Successful");

[Forms]![Login]![User_Name] and [Forms]![Login]![Password] are from a form that asks for your user name and password. The IFF statement is comparing your user input to those fields and finding a match in the table. This query is successful. I want to INSERT the results into the LOGIN FIELD of the LOGIN_RESULTS table. I try this:

INSERT INTO Login_results (login)
SELECT G.Results
FROM (SELECT IIf([Forms]![Login]![User_Name]=[C_Customers].[user_name]                                       
             And                                                
             [Forms]![Login]![Password]=[C_Customers].[Password],"Login Successful","Login Failed") AS Results FROM C_Customers)  AS G
WHERE ((G.Results)="Login Successful");

However, the data never appears in the table. Login_Results is empty with one field, Login.

EDIT: That was random. The only thing I changed was the Name of the table changed from 'Login_Results' to 'Login_Status' and the field from 'Results' to 'Status' because I liked it more. Suddenly it now appends. Why? I don't know.

Here's the code that now runs for some reason.

Insert into Login_Status (Status)
SELECT G.Results
FROM (SELECT IIf([Forms]![Login]![User_Name]=C_Customers.user_name             
                  And                                             
                [Forms]![Login]![Password]=C_Customers.Password
                ,"Login Successful"
                ,"Login Failed") AS Results 
       FROM C_Customers)  AS G
WHERE (((G.Results)="Login Successful")) OR (((G.Results)="Loggin Failed"));

New thread topic: What did I change that suddenly made the code run? It never threw up a "Couldn't find table/field" error so I don't see why a changed in target field/name would work.


回答1:


You cant try using EVAL for multiple comparisons in the IIf function

I still can't figure out what you're trying to save in the table - there's not customer info with any of the Login Success inserts. But maybe you're just trying for a proof of concept

I also think you have a typo in your WHERE clause for (G.Results)="Loggin Failed" - should that be Login Failed?

That might be skewing your results

In any case you can try this example using EVAL

INSERT INTO Login_Status (Status)
SELECT G.Results
FROM 
    (SELECT 
        IIf(EVAL(([Forms]![Login]![User_Name]=C_Customers.user_name) AND ([Forms]![Login]![Password]=C_Customers.Password)),
                "Login Successful", "Login Failed")
                AS Results 
       FROM C_Customers)  AS G
WHERE (G.Results="Login Successful") OR (G.Results="Login Failed");


来源:https://stackoverflow.com/questions/50165745/insert-into-based-on-the-results-of-an-iif-statment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!