Skip to content
Advertisement

Writing Query statement with a String and a Date in Excel VBA

Using an ADODB connection, I have an excel database where I store data, and I am trying to make a query in one of the sheets(db_Reports). Values for Lastname and Birthday are entered in textboxes (tb_Lastname & tb_bday, respectively) from a userform.

My problem is that the first two query statements work (commented in this case): one queries Lastname which gets a string, the other queries a date. Each of them works separately, but if I combine them since I need to query both name or date in the same search, it is giving me Type Mismatch; Runtime error 13. I have been searching anywhere but I can’t figure out how can I make this work. I even tried to convert date into a string but still, it failed.

Basically, how can you make a query for both a string and a date in one line?

I apologize for the long code line, the lingo I used, and even the formulation of the question as I am a self-taught newbie.

Advertisement

Answer

It seems that ‘[Birthday] = ‘” & CStr(CLng(CDate(tb_bday))) &’ creates an error when no value is entered in the textbox, tb_bday. I cant explain why. As a workaround, I just created an if-then statement. I still would want to avoid using if-then if it can be coded in one line instead.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement