Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How would a loop be written in C# that uses DFirst?

I am developing a C# Windows platform application using VS2005.

How would you rewrite the following VBA code in C#?

For intBranch = 1 To 999
tst = DFirst("[CPS Account Number]", "tblFlLNExp", "Left([CPS Account Number],3)='" & Format(intBranch, "000") & "'")
If Not IsNull(tst) Then
           strSQL = "INSERT INTO tblFlLNMod SELECT tblFlLNExp.* FROM tblFlLNExp WHERE Left([CPS Account Number],3)='" & Format(intBranch, "000") & "'"
for (int intBranch = 1; intBranch < 1000; intBranch++)
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

In your VBA example you are using data stored on an excel spreadsheet, for the C# where will this data be stored?

If it is still in an Excel spreadsheet why try to use to C# at all. While C# is a more powerful programming language, VBA has been developed specifically for use with MS Office and personally I believe you are better off leaving the code in VBA.

Avatar of zimmer9


I don't have a choice. My company is dropping support for MS Access so my apps have to be rewritten in C#.
Avatar of zimmer9


The code is based on SQL Server 2005 tables.
Avatar of zimmer9


There are no Excel files involved here.

The syntax for the DFirst function is:
DFirst ( expression, domain, [criteria] )

expression is the field from which you want the first value.
domain is the set of records. This can be a table or a query name.
criteria is optional. It is the WHERE clause to apply to the domain.

In other words, it is a SELECT TOP(1) clause in SQL.

In .NET, you will replace this with SqlCommand.

This is just an example of how you should go with this:

SqlConnection conn = new SqlConnection("ConnectionString");

for (int intBranch = 1; intBranch < 1000; intBranch++)
    SqlCommand cmd = new SqlCommand("SELECT TOP(1) [CPS Account Number] FROM          
    tblFlLNExp WHERE [CPS Account Number] LIKE '%" + intBranch.ToString() + "%'", conn);


Please let me know if anything.

Avatar of zimmer9


How would you make the comparison so that the intBranch value are 3 positions in length with leading zeroes? (for ex: 001, 002 ... 099, ...999)

Because the values in Left([CPS Account Number],3) =  001, 002, 003
Avatar of zimmer9


I tried the following but it doesn't translate to the first value being 001

sql = "SELECT TOP(1) [CPS Account Number] FROM tblFlLNExp WHERE [CPS Account Number] LIKE '%" + intBranch.ToString("000") + "%'";
Avatar of zimmer9


Would I need to cast it to int and keep the zeros to create a copy of it and then cast it to int, then have 2 versions of it, one as int and one as string?
Avatar of zimmer9


intBranch num = 001; // 3 digit number

string sNum = num.ToString("000");

How would I incorporate the above into my loop if this is what needs to be done?
Avatar of igordevelop
Flag of North Macedonia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zimmer9


My goal is to create values from 001 to 999 and by using

intBranch.ToString() + "000";    

the values generated would appear to be 1000, 2000, 3000, 4000, ... 50000 ... 400000 ...

up to 999000
Avatar of zimmer9


What I'm trying to state is that Left([CPS Account Number],3) has values
such as 001, 002, 003, 004, 005 up to 999.

So the intBranch would have to have values such as 001, 002, 003, 004, 005 up to 999
to create a match when comparing the 2 values.