Search code examples
ms-accessvbams-access-2016

Use the sum of two text box values as a parameter for select top statement in access


I need help with this SQL line...I have two text box named [txt_ibn_hayyan] and [txt_ibn_sina] in a form named [student_names]. What I want to do is to use the sum of these text box values as a parameter for a select top N statement as shown in the code VB code below:

strSQL="select TOP "& [forms]![student_names].[txt_ibn_hayyan]+[forms]![student_names].[txt_ibn_sina]& " * " _
& "FROM sometable

what happens here that when the values of the two text box are 4 and 3 for example, the resulted parameter will be 43 instead of 7 (which is what I want). Is there a way to sum the value of these two text box instead of joining them as a text?


Solution

  • That's because your text boxes contain strings. Cast them to numbers, and it will work fine:

    strSQL="select TOP " & CLng([forms]![student_names].[txt_ibn_hayyan])+CLng([forms]![student_names].[txt_ibn_sina]) & " * " _
    & "FROM sometable