Solved

get a record and update that record

Posted on 2013-02-04
10
369 Views
Last Modified: 2013-02-05
I am having a serious mental block.  I need to retrieve a record from my DB based on a query

dim rs as adodb.recordset
dim sSQL as string

set rs = new.adodb.recorset
sSQL = "Select balance from ctbal" & " where ctbalID = " & Me.txtID

when I debug print this I get:

Select balance from ctbal where ctbalID = 1

if the field in question "Balance" has a value say 2 then why does my code say "empty" when I place my cursor over the field.

Here is exactly what I am trying do accomplish:

1) Extract the field value of "balance" from my table CTbal.
2) Add to the field from my form to create a "new" balance
 3) Update my table to reflect the "new" balance
0
Comment
Question by:sptech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:esolve
ID: 38850821
What is the datatype of the ctbalID column? If it is not numeric then your query should look like this:

sSQL = "Select balance from ctbal " & " where ctbalID = '" & Me.txtID & "'"

Select balance from ctbal where ctbalID = '1'
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 38850844
Hi,

Seems like one step to me.

Sim strSQL as string
strSql = "UPDATE ctbal SET balance = (balance + " & me!.fieldfrommyform & ") where ctbalID = '" & Me!txtID & "'"
currentdb.execute strSql

No need to create a recordset - just do the update.

Regards,

Bill
0
 

Author Comment

by:sptech
ID: 38850909
the field inside the table "balance is a HH:NN (short time) field as is the field on my form.  The balance field inside my table is 01:00.  The me.fieldname is 10:25 (also short time)

when I try the Balance+me.fieldname I get (10:2501:00)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Bill Ross
ID: 38851012
Hi,

If you are trying to add time then you must add correct delimiter.  Try this:

strSql = "UPDATE ctbal SET balance = (balance + #" & me!fieldname & "#) where ctbalID = '" & Me!txtID & "'"
 
Date/time requires #

Regards,

Bill
0
 

Author Comment

by:sptech
ID: 38851120
Bill,
  I got the time issue resolved (it isn't as clean as yours but it works)(I will try your solution in the morning).  

I am getting an error when I try to execute the strsql string you created for me.  I think it has to do with currentdb.execute, strsql.  What should I have done initially to get the db ready to update?
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 38851690
Hi,

What is the error message?

What is the table structure of ctbal?

currentdb.execute does not need any prep - it just runs.

Regards,

Bill
0
 

Author Comment

by:sptech
ID: 38853794
I am getting "argument not optional"  the field is a date/time field with short time. when I hover over the me.fieldname instead of seeing 10:25 (short time) I see 10:25:00 am. I am using ADO and not DAO can this be part of the issue?
I have the sub listed below:

Private Sub CTBal()
Dim sSQL As String

On Error GoTo err_label

sSQL = "update ctbal set balance = (#" & balance & "# + #" & Me!txtTCTAppr & "# ) where ID =  ' " & Me!txtID & " ' "
Debug.Print sSQL
CurrentDb.Execute , sSQL


Exit Sub
   

err_label:
    MsgBox "Error creating new record" & vbNewLine & Err.Description, vbCritical, "Error creating new record"

End Sub

the results of the debug.print:

update ctbal set balance = (#1:00:00 AM# + #10:25:00 AM# ) where ID = '1'
0
 

Author Comment

by:sptech
ID: 38854378
Bill
  A little clarification on what I am trying to accomplish.

My users travel while traveling they earn comp time vice over time.  I am trying to keep track of how much they have earned, used and the balance.  I can track how much earned, how much used, but the balance issue is kicking my behind.  In my earlier comment I have an existing balance of 1 hour, my user earned 10 hours 25 minutes for a grand total of 11 and 25 minutes.  "Balance" shows when I hover the cursor to be 11 hours and 25 minutes, but the currentdb.execute, ssql is throwing the error "argument not optional"

to add a monkey wrench I will have to eventually add logic to round to the nearest quarter hour.
0
 
LVL 14

Accepted Solution

by:
Bill Ross earned 500 total points
ID: 38855511
Hi,

I don't think it's a good idea to keep results of calculations in a table.  If you know how much they have earned (tblEarnedTime) and how much they have used (tblTimeUsed) ou can always get the balance.  No need to try to update the balance as you go.  Look at your checkbook as an example.

A query is an easy tool to use for the answer and subtract the total of one from another.

The expression below will give you decimal hours rounded to the nearest .25 hours between 2 time fields (me!fldStop and me!fldStart) and set the fldNearestQtrHour field.

me!fldNearestQtrHour=round(((me!fldStop-me!fldStart1) * 60 * 24) / 60 /.25,0) * .25

Put this code in the after update event of your form to save the hours earned and hours used.  Use a query anytime the balance is needed.

Regards,

Bill
0
 

Author Closing Comment

by:sptech
ID: 38858201
Bill gave me answers to two issues in this question. I greatly appreciate his helping a newbie such as myself.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question