473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

If statement ref to db table and saving result to db table

Hi guys.

I’m not familiar with Ms access VB (or any other for that matter)but learning fast. I have tried various methods to achieve the outcome but all have failed.

Background
Front End developed using MS Access Forms which is connected to SQL Server 2005 DB. Allowing users to capture Customer info.

Objective
On value input in on the form it needs to make reference to a scorecard (on db), check the input against the scorecard variables and once matched the points associated with the value to be written to another table in db.
I’m hoping I’m not confusing the matter.
Below is one attempt(failed horribly). Don’t have any idea of how to resolve or achieve the outcome. Would appreciate any assistance

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_AfterUpdate()
  2. Dim db As DAO.Database
  3. Dim SC As DAO.Recordset
  4. Dim OutPut As DAO.Recordset
  5. Dim Value As Integer
  6.  
  7.   Set db = CurrentDb()
  8.   Set SC = db.OpenRecordset("dbo_BBRS_SC", dbOpenSnapshot)
  9.   Set OutPut = db.OpenRecordset("dbo_Cust_RG_Scr_Values", dbOpenSnapshot, dbSeeChanges)
  10.  
  11.   Value = Customer!CB_Industry
  12.  
  13.   If SC.RecordCount = 0 Then Exit Sub
  14.  
  15.   SC.MoveFirst
  16.   ' loop through each record in the first recordset
  17.   Do Until SC.RecordCount = 11 Or ctr > 11
  18.  
  19.       If Value = SC!ID_LKVar Then
  20.         OutPut.AddNew
  21.         OutPut![Score] = SC![Score]
  22.         OutPut.Update
  23.  
  24.     Else
  25.          If Value <> SC!ID_LKVar Then
  26.         OutPut.AddNew
  27.         OutPut![Score] = 99
  28.         OutPut.Update
  29.       End If
  30.  
  31.         ctr = ctr + 1
  32.   Loop
  33.  
  34.   SC.Close
  35.   OutPut.Close
  36.   Set SC = Nothing
  37.   Set OutPut = Nothing
  38.   Set db = Nothing
  39.   End If
  40. End Sub
Dec 7 '10 #1

✓ answered by patjones

Hi Janine,

Thanks for explaining what you're trying to do. It is more or less what I was picturing but it is always good to have clarification.

Before the Do Until loop executes, how many records does SC have in it? I ask this because I believe that your loop is not running.

In addition, as I pointed out yesterday I believe that using the dbOpenSnapshot parameter when opening the OutPut recordset might problematic for you. Try changing dbOpenSnapshot to dbOpenDynamic.

Finally, it looks to me like the placement of the End If on line #39 is not correct. I believe that it needs to be on line #30 just below the End If on line #29. The way it is now, the Do loop terminates before the first If statement inside ends. In fact, I'm surprised that even compiles.

Pat

13 2155
NeoPa
32,556 Expert Mod 16PB
Bonjour Janine (That's all the French I'm allowed due to site regs).

I'm impressed with the way you've laid out the question, so I will certainly try to help. I need you to try to explain in ordinary language though, what it is you are trying to do. Some example data may be helpful in this.
Dec 7 '10 #2
patjones
931 Expert 512MB
Hi Janine,

As NeoPa points out, a little more information about what you are trying to do (more context) would be useful. In addition, I would ask exactly what is happening to indicate that this is not working. Are you getting a particular error at some point in the code? Looking at your code, I see two things immediately that could cause problems.

The first is that on line #9 you are using the dbOpenSnapshot flag to open the recordset OutPut; however, dbOpenSnapshot is used to open a recordset with fixed records, but you are attempting to change it on lines #20, 21, and 22 by adding a record to it.

The second problem I see is that you are using the RecordCount property in your criteria for the Do Until on line #17. The RecordCount property for a recordset is fixed (unless you are adding or deleting records to it, which is not the case for SC). If SC already has 11 records going into that Do Until comparison, nothing in the loop will execute. You want to loop through SC record by record until you reach the end, so I suspect that you want something more like

Expand|Select|Wrap|Line Numbers
  1. Do Until ctr > 11
  2. . . .

Third, you have an End If on line #39 which does not seem to belong there. When you use the one-line version of If...Then as on line #13, the End If is not necessary.

I hope this helps.

Pat
Dec 7 '10 #3
Back to business
I don’t receive any error msg but the code appears not to find a match. Which is not the case. I will attempt to provide some more detail.

Objective: Design, build and implement a Risk Grade Evaluation Tool which will be used by Credit Managers (Banking Industry) indicating Risk level by means of Grading scale for a specific customer business segment based on customer input data (i.e. Industry, Management of Business, Bank account behaviour, Financial information : Audit statements)

Data Structure (portion of it)
Table 1 : Customer Table
Data Captured by Credit Bank Manager in Branch by (Form)
Data fields i.e Date; Customer Number; Industry Type Total Assets est.

Table 2 : Credit Scorecard Parameters
Parameters for above input data and each with their own ranges. i.e. (will just give 2 examples, in total over a 100 are used)
Score Variable Band/Ranges Points
Industry Type : Charity -10
Local Government -20
FinancialTotalAsset: <250 -25
600 5

With all that said. On input the customer record would look something like this
CustNumber IndusType TotAssets
123344 Charity 300

As soon as the user enters the data in the form I want the loop to execute and the points associated with the variable to be stored on different table. i.e.
CustNumber Variable Points
123344 Charity -10
123344 TotAssets 5*
* 300 > than max of 250 and < 600.

Might have elaborated too much but do think its better to provide a picture of whats happening as a whole, to ensure I use the right code in achieving this.

Regards
Janine
Dec 8 '10 #4
patjones
931 Expert 512MB
Hi Janine,

Thanks for explaining what you're trying to do. It is more or less what I was picturing but it is always good to have clarification.

Before the Do Until loop executes, how many records does SC have in it? I ask this because I believe that your loop is not running.

In addition, as I pointed out yesterday I believe that using the dbOpenSnapshot parameter when opening the OutPut recordset might problematic for you. Try changing dbOpenSnapshot to dbOpenDynamic.

Finally, it looks to me like the placement of the End If on line #39 is not correct. I believe that it needs to be on line #30 just below the End If on line #29. The way it is now, the Do loop terminates before the first If statement inside ends. In fact, I'm surprised that even compiles.

Pat
Dec 8 '10 #5
NeoPa
32,556 Expert Mod 16PB
This is hard to follow Janine. There are various entities which are names with spaces embedded within them. As you don't indicate where the names end it's hard to read you.

Is there maybe a table called [CustNumber Variable Points]? If so, then the details of this table are missing and probably (with my still very hazy understanding of what you're trying to do) quite important.
Dec 8 '10 #6
I have made the changes to the code reflecting "dbOpenDynamic" and moving the "End if" . I now recieve a Run time Error 3001 - Invalid Argument
on the below
Set OutPut = db.OpenRecordset("dbo_Cust_RG_Scr_Values", dbOpenDynamic, dbSeeChanges)
Dec 9 '10 #7
Apologies. the table is to be the "output table" with the fields [CustNumber]; [Variable]; [Points] and the input for it is to be derived from the loop.
Dec 9 '10 #8
patjones
931 Expert 512MB
Hi Janine,

I see that you indicated the question as being answered, but from your last two posts it seems that is not the case. Are you still getting the 3001 runtime error? If you are, then I will have to look into that.

Pat
Dec 9 '10 #9
Apologies for that. Still getting the error.
Dec 10 '10 #10
Hi All.

Is it possible to called a SQL stored procedure in a form? I orginaly created a sql Stored proc and then later on attempted to replace it with the VB loop. Could I have your opinion on this in terms of which would be best practice ?
Regards
Janine
Dec 13 '10 #11
patjones
931 Expert 512MB
Hi Janine,

I apologize for being so slow in replying to you. Could you try doing this:

Expand|Select|Wrap|Line Numbers
  1. Set OutPut = db.OpenRecordset("dbo_Cust_RG_Scr_Values", , dbSeeChanges)

and seeing if you still get the error?

Pat
Dec 15 '10 #12
Hi Guys

I have amended the code and the below seems to be working but the problem now is by the "Add new" with Error" Object read only". Not the case. Please assist !

Expand|Select|Wrap|Line Numbers
  1. Private Sub BTCustValidation_Click()
  2. Dim MyDB As DAO.Database
  3. Dim SC As DAO.Recordset
  4. Dim Output As DAO.Recordset
  5. Dim GRPID As Variant
  6. Dim CustNum As Variant
  7. Dim SubVarID As Variant
  8. Dim Score As Variant
  9. Dim ScDate As Double
  10. Dim i As Integer
  11.  
  12. Set MyDB = CurrentDb()
  13. Set SC = MyDB.OpenRecordset("dbo_BBRS_SC")
  14. Set Output = MyDB.OpenRecordset("dbo_Cust_RG_Scr_Values", , dbSeeChanges)
  15.  
  16. GRPID = SC![GRP_ID]
  17. CustNum = Cust_Number
  18. SubVarID = SC![Sub_Var_ID]
  19. Pionts = SC![Score]
  20. ScDate = Date
  21. 'ORG TYPE'
  22.  
  23. For i = 0 To SC![ID_LKVar] = 11#
  24. If ID_OrgType = SC![ID_LKVar] Then
  25. Output.AddNew
  26.     Output![GRP_ID] = GRPID
  27.     Output![PK_Customer] = CustNum
  28.     Output![Sub_Var_ID] = SubVarID
  29.     Output![Score] = Pionts
  30.     Output![Score_Date] = ScDate
  31. Output.Update
  32.     End If
  33. Next
  34. For i = 0 To SC![ID_LKVar] = 11#
  35. If ID_OrgType <> SC![ID_LKVar] Then
  36. MsgBox "No Match"
  37. End If
  38. Next
  39. End Sub
Dec 20 '10 #13
patjones
931 Expert 512MB
Hi Janine,

I haven't worked with SQL Server back-ends very much, but I'm just wondering if there is something in the SQL Server settings for this table that might prevent additions and updates? I cannot see anything in this code that would cause this error.

Pat
Dec 22 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
2
by: hubert.trzewik | last post by:
Hello, Is it possible to EXEC stored procedure from a query? I want to execute stored procedure for every line of SELECT result table. I guess it's possible with cursors, but maybe it's...
5
by: aboycalled3 | last post by:
I'm interested in using the fascinating CSS available at http://www.moronicbajebus.com/playground/cssplay/reformat-table/ which allows one to present tabular data in a way that's more appealing...
5
by: AFN | last post by:
Hi. I'm pretty comfortable binding a single recordset result to a datagrid. But now I have a more unique problem. I have 2 really long stored procedures that cannot be combined at the SQL...
8
by: gacuna | last post by:
i want to insert into a temporal table the result of a store procedure. on sql server the sentence would look like this (already working) INSERT INTO #SHIPINFO exec TESTDTA.S59RSH05 @SCBILLTO,...
1
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into Access belonging to BMW dealers in Europe. ...
1
by: rhepsi | last post by:
HII all, im working on postgresql database where i want to copy the data from one table to other table.... when im trying to write the sql query.. ERROR: 42601: syntax error at or near...
2
by: cynderborg | last post by:
My database is a tool to write an extensive report. I need the ability to pull a canned response from one table into another table if the user wants to adopt the text. I have the form opened that...
3
by: JBurgett | last post by:
How can I place the results of a dynamically generated select statement into a (#) temp table? This has me stumped, any help would be greatly appreciated. (The names used in the following...
2
by: nikolasapl | last post by:
Is there a command so that instead of displaying the result table (for example the result of the following query) in the grid tab in the SQL Server Analyzer environment, to export it into an .xls...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.