By using VBA Access ,I want to copy excel and rename it depending on Table first field.
In below example,
For example, instead of using fixed name like:
FileCopy "C:Documents\Folder\CurrentFile.xlsx", "C:\Documents\New Folder\NewFile.xlsx"
I want to use:
FileCopy "C:Documents\Folder\CurrentFile.xlsx", "C:\Documents\New Folder\NewFile" & [Table1]![Feild1] & ".xlsx"
Your question is unclear, but if [Table1]![Field1] returns the string to replace only CurrentFile then you could use:
FileCopy "C:\Documents\Folder\CurrentFile.xlsx", _
"C:\Documents\Folder\" & [Table1]![Field1] & ".xlsx"
...so:
C:Documents was missing a \
NewFile" & [Table1]![Feild1] & ".xlsx" was also missing a \
[Feild1] may need to be spelled [Field1] (unless the misspelling was intentional)
Related
I have multiple combo & text boxes to search for different values in my main table on my front end. The code is as follows, just replicated for different types etc. This all works fine.
If Not IsNull(Me.strSearch) Then
strWhere = strWhere & "([tbl_Main.Description] Like ""*" & Me.strSearch & "*"") AND "
End If
My Problem is, I'm trying to create a text box which searches 2 columns simultaneously in my tbl_Main (tbl_Main.LessonsLearnt & tbl_Main.RecommendedAction) but can't figure out how to modify my current code to add another column in, searchable from the same textbox.
Disclaimer: I'm very much a beginner in MS Access - so please keep the explanation as simple as possible :D
If you need any other info - just let me know!
Thanks in advance
strWhere = strWhere & "(tbl_Main.Description Like '*" & Me.strSearch & "*' OR tbl_Main.OtherField Like '*" & Me.strSearch & "*') AND "
This will search for the strSearch being in either Desscription or OtherField. I also replaced your double double quotes with single quotes for better code readability and cross compatibility with other DBMS and removed the brackets that are only needed if you have spaces in your table/field names (something you really should never do anyway).
I have a set of tables from a client that are supposed to be Cyrillic, but I guess the original coding was wrong or not set. All the text is gibberish.
If I past the text into an html page and set the encoding to Cyrillic-1251, I see the text as it should be.
Before: Ñèíèöûí À.Â.
After: Синицын А.В.
I've been looking for a VBA solution to convert the text in the tables without success. I thought this would be quick & easy, but so far no luck.
I'm running Win 7 with Access 2010
If you don't know Access, but have any VBA function to do this, I can adapt it to my needs.
Any help would be appreciated.
One possible way to solve the problem would be to export the table to CSV as code page 1252 ("ANSI") and then import it as code page 1251. I just tried that and for an existing table named [OldTable]
the following VBA code
Option Compare Database
Option Explicit
Sub DiskBounce()
Const tempFilePath = "C:\Users\Gord\Desktop\foo.csv"
DoCmd.TransferText _
TransferType:=acExportDelim, _
TableName:="OldTable", _
FileName:=tempFilePath, _
HasFieldNames:=True, _
CodePage:=1252
DoCmd.TransferText _
TransferType:=acImportDelim, _
TableName:="NewTable", _
FileName:=tempFilePath, _
HasFieldNames:=True, _
CodePage:=1251
Kill tempFilePath
End Sub
produced the following [NewTable]
I think my first submission might have been a bit confusing so I decided to rewrite it to better explain what I am attempting to do.
I have an Access database. In it there are two combo boxes, one containing all possible forms of annuity (that I'm dealing with) and the other combo box is the ones the user populates that apply to that particular client. This master list is fed from a table (tblPickAnnuityForms). This table has 3 fields, Name, VarName (these values match to another table), and Deleted (is a 1 or a 0 depending on if the user selected it). I also have a second table called tblPlanSpecs. This table, among other fields, has the fields that match up to the VarName field in the first table.
What I need to do, since tblPickAnnuityForms is basically a temporary table and it changes as you move between records (clients), is to repopulate it when you go to that record based on the values in tblPlanSpecs. Once tblPickAnnuityForms is populated based on the previous elections for this record (client), the two combo boxes are re-queried to display the proper values (ie the left box shows the remaining, unused annuity forms and the right box shows the forms that apply to this client.
Below is my attempt at doing this.
Dim db As DAO.Database
Dim rsList As DAO.Recordset
Dim rsData As DAO.Recordset
Dim CurrForm As String
Dim FormVal As Integer
Dim Plan As String
Set db = CurrentDb
Set rsList = db.OpenRecordset("tblPickAnnuityForms", dbOpenSnapshot)
Plan = [Forms]![FrmHome]![PlanNameCalc].Value
Set rsData = db.OpenRecordset("SELECT tblPlanSpecs.LifeAnnuity, tblPlanSpecs.FiveCC, tblPlanSpecs.TenCC, " _
& "tblPlanSpecs.FifteenCC, tblPlanSpecs.TwentyCC, tblPlanSpecs.FiveCertain, tblPlanSpecs.TenCertain, " _
& "tblPlanSpecs.FifteenCertain, tblPlanSpecs.TwentyCertain, tblPlanSpecs.FiftyJS, tblPlanSpecs.SixtySixJS, " _
& "tblPlanSpecs.SeventyFiveJS, tblPlanSpecs.HundredJS, tblPlanSpecs.MCR FROM tblPlanSpecs " _
& "WHERE tblPlanSpecs.PlanName='" & Plan & "'")
Do
CurrForm = rsList.Fields("VarName")
FormVal = rsData.Fields(CurrForm)
DoCmd.RunSQL ("UPDATE tblPickAnnuityForms " _
& "SET tblPickAnnuityForms.Deleted=" & rsData! & FormVal & " " _
& "WHERE (((tblPickAnnuityForms.VarName)='" & CurrForm & "'))")
MsgBox (CurrForm & "changed to " & FormVal)
rsList.MoveNext
Loop Until rsList.EOF
If Not rsList Is Nothing Then
rsList.CLOSE
Set rsList = Nothing
End If
If there is a better solution, perhaps I can go in a different direction. Currently this is bombing out in the loop where it says rsData! & FormVal It does not like using a variable to call a field. Ideally I would like to avoid specifically calling every variable by name in code when populating rsData. In other words, I want this to work no matter how many other options I add to my master list for the combo box, without going back in to add more items to select query.
Please let me know if I am unclear in my intended direction or methods. I could really use the help figuring out what is wrong.
Well, it seems I solved my own problem. I had so many ideas running through my head, I half-implemented one and forgot. The variable FormVal was already pulling the value I needed from rsData. I then tried to pull the value again using FormVal as the field variable. Anyway, below was the simple solution and everything works now.
DoCmd.RunSQL ("UPDATE tblPickAnnuityForms " _
& "SET tblPickAnnuityForms.Deleted=" & FormVal & " " _
& "WHERE (((tblPickAnnuityForms.VarName)='" & CurrForm & "'))")
In other words, I didn't need to do rsData![ & FormVal & ] (which I am sure is improper syntax) I just needed to use FormVal by itself.
I have two different MDB files [with the same structure]. Is there an existing tool that can report the difference between the two files [row by row]?
I found a program called "MDBDiff" on SF, however the program is no longer available for download.
I've made an AccdbMerge utility that is able to compare data and programming objects as well. In scope of "row by row" comparison - it will show what records were added/modified/removed, for modified records it will highlight fields with updated values.
See the following page and go down a bit for a list of utilities to compare Access databases
http://www.granite.ab.ca/access/thirdparty.htm One of those might be what you're looking for.
I wanted to do the same (basically use DIFF to see differences row by row) so
1) I exported all the tables:
Option Explicit
Option Compare Database
Private Sub ExportAllTables()
Dim myDatabase As Database
Dim myTableDef As TableDef
Dim strTableName As String
Set myDatabase = CurrentDb
For Each myTableDef In myDatabase.TableDefs
DoEvents
strTableName = myTableDef.Name
DoCmd.TransferText _
acExportDelim, _
, _
strTableName, _
Environ("USERPROFILE") & "\DeskTop\dump\" & strTableName & ".CSV", _
True
Next myTableDef
MsgBox "Done"
End Sub
2) concatenated them into one file
type *.csv > all.txt
CAT will do as well if you have it
3) diff'ed them
diff all.txt all2.txt
Try using SQL Data Compare from Redgate, http://www.red-gate.com/products/SQL_Data_Compare/index.htm
and then use this trick,
http://www.red-gate.com/messageboard/viewtopic.php?p=15296#15296
I haven't tried it yet but this tool looks like it would do the job http://www.datanamic.com/download/download-datadiff-for-msaccess.html
I have an access 2007 Database that outputs a report in excel format, the report is dependent on a date parameter that is chosen by the user. This parameter is selected via a textbox (text100) that has a pop up calendar. I would like to use the date in the text box(text100) as part of the filename. I am using the transferspreadsheet method to create the export, However I do not need the column headers. Once the file is created I have the code open the file and delete the headers. Also the current code is using todays date in the filename which is not accurate. The filename needs to reflect the date that was selected by the user in the text box from the pop up calendar
Ok here is the code.
Sub Branch298nohdr()
Dim Filename As String
Dim Path As String
Dim Branch As Integer
Dim Text100 As Date
Dim xl
Branch = "298"
Path = "Path" & Branch & "\"
Filename = "Identity Report " & Branch & " " & _
Replace(Text100, ":", " ") & ".xls"
If Dir(Path & Filename) <> "" Then
MsgBox "File has been created already"
If Dir(Path & Filename) <> "" Then
GoTo 53
End If
Else
Set xl = CreateObject("excel.application")
TempVars.Add "branchnum", Branch
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
"queryname", Path & Filename, False
xl.workbooks.Open Path & Filename
With xl
.Rows("1:1").entirerow.Delete
.Columns("L:L").select
.Selection.NumberFormat = "0"
.range("a1").select
xl.workbooks(1).Close Savechanges:=True
xl.Quit
Set xl = Nothing
53
MsgBox "Done!"
End With
TempVars.Remove "branchnum"
End If
Branch298nohdr_Exit:
Exit Sub
End Sub
Text 100 is where the user selects a date via a pop up calendar. I would like to use this date as part of the file name. Currently using the text100 as part of the filename it is being referenced as 12:00 am, and then adds this to the file name. I hope this clears up my intention.
Text 100 gets set on the opening form then there are several buttons which allow the user to pick between several branches or all branches.
Well the obvious question is, where does Text100 get first set?
Another style comment, it is better to do
goto ExitSub
'...
ExitSub:
Then your "GoTo 53" - its a little more meaningful.
At that rate, it would be better to move your "Done" message outside of the IF statements, and the TempVars doesn't seem to have a purpose; remove it.
Edit:
I presume if Text100 is a textbox on the form, then the line that reads:
Dim Text100 As Date
is going to override that reference in your code.
If you are referencing that textbox in your code, you need to do it this way:
foo = me.Text100
' or
foo = Forms!FormName.Text100
It's a little tough to determine exactly what your question is, but I think you are asking, "how do I use the contents of a text box as an export file name?" It sounds like somewhere in your code it creates a string for the filename that has & now() tagged on to the end to use the current date in the filename. Can you simply replace the '& now()' with '& textbox.value'?
A JD Long said it is hard to see any question in your posting. Maybe you should edit it again.
But as a general remark you need to escape any special characters that the user entered before you are going to use the input in a file name.
The following reserved characters are not allowed:
< > : " / \ | ? *
For more details on naming files in Windows see: Naming a File or Directory in MSDN.